Given a BST and a value, insert the value into the BST and return the root. The new node always becomes a leaf — you never need to restructure the tree.
The insertion algorithm is just search until you find an empty spot:
None, create a new node hereval < node.val, insert into the left subtreeval > node.val, insert into the right subtreeThe recursive solution is beautifully simple — each recursive call either goes deeper or creates the new node.
inorder(root)
[1, 2, 3, 4, 7]
inorder(root)
[1, 2, 3, 4, 5, 7]