Key Benefit: BSTs enable O(log n) insertion/removal (assuming a balanced tree) vs. O(n) for sorted arrays.
null, create a new node and return it to the parent.Tree: 4 (root) → right: null
Steps:
null → Insert 6 as the right child.Result:
4
\\
6
def insert(root, value):
if not root:
return TreeNode(value) # Base case: create new node
if value > root.val:
root.right = insert(root.right, value) # Assign to right
elif value < root.val:
root.left = insert(root.left, value) # Assign to left
return root
null).