Insert into a binary search tree
In a binary search tree every value in a node's left subtree is smaller and every value in its right subtree is larger. Inserting walks down as a search would and attaches a new node where the search falls off the tree.
# 8 has 3 on the left and 10 on the right
# inserting 6: 6 < 8 go left, 6 > 3 go rightThink before coding
Returning the root from the recursive call lets an empty subtree become a new node without special cases. Ignoring duplicates keeps every value unique. An in-order walk of the result lists the values in sorted order. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
TreeNode(value) is provided. Write insert(root, value) returning the root after inserting value; insert into None to start a tree. Ignore a value that is already present.
The explanation opens here once every check passes.