You are given the root node of a binary search tree (BST) and a value to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
Binary Search Trees (BSTs) maintain the property that for any node:
All values in the left subtree are smaller than the node’s value.
All values in the right subtree are greater than the node’s value.
To insert a new value into the BST:
Traverse the tree starting from the root.
Compare the new value to the current node’s value:
If the value is smaller, move to the left subtree.
If the value is larger, move to the right subtree.
Once a None (or null) position is found in the correct subtree (where the new value logically fits), create a new node with the value and assign it to this position.
The problem guarantees that the new value does not exist in the BST, so we don’t have to handle duplicates.
classSolution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root ==null) {
returnnew TreeNode(val); // Base case: if root is null, create and return a new node }
if (val < root.val) {
root.left= insertIntoBST(root.left, val); // Recursive case for left subtree } else { // val > root.val root.right= insertIntoBST(root.right, val); // Recursive case for right subtree }
return root; // Return the unchanged root }
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
definsertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
ifnot root:
return TreeNode(val) # Base case: if root is None, create and return a new nodeif val < root.val:
root.left = self.insertIntoBST(root.left, val) # Recursive case for the left subtreeelse:
root.right = self.insertIntoBST(root.right, val) # Recursive case for the right subtreereturn root # Return the unchanged root
⏰ Time complexity: O(h) where h is the height of the BST. In the worst case (unbalanced tree), h is n (number of nodes). For a balanced BST, h is log(n).
🧺 Space complexity: O(h) in the recursive approach because of the stack frames in recursion. The iterative approach uses constant space O(1).
classSolution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root ==null) {
returnnew TreeNode(val); // If root is null, create the new node and return }
TreeNode cur = root; // Start traversing from the rootwhile (true) {
if (val < cur.val) {
if (cur.left==null) {
cur.left=new TreeNode(val); // Found the insertion pointbreak;
} else {
cur = cur.left; // Move to the left subtree }
} else { // val > cur.valif (cur.right==null) {
cur.right=new TreeNode(val); // Found the insertion pointbreak;
} else {
cur = cur.right; // Move to the right subtree }
}
}
return root; // Return the unchanged root }
}
classSolution:
definsertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
ifnot root:
return TreeNode(val) # If the root is None, create and return a new node cur = root # Start traversal from the rootwhile cur:
if val < cur.val:
ifnot cur.left:
cur.left = TreeNode(val) # Insert into the leftbreakelse:
cur = cur.left # Move to the left subtreeelse: # val > cur.valifnot cur.right:
cur.right = TreeNode(val) # Insert into the rightbreakelse:
cur = cur.right # Move to the right subtreereturn root # Return the unchanged root