Algo-Sim › Binary Trees & BST
Insert into a Binary Search Tree
Medium · Binary Trees & BST · step through it in Java, Python and C++ on your own input
Search for the value until the walk falls off the tree, then attach the new node as a leaf there.
Problem
Insert val into a binary search tree and return the root. val is not in the tree yet. Any valid BST is accepted; the simple way adds it as a new leaf.
Constraints
- 0 ≤ number of nodes ≤ 10⁴
- -10⁸ ≤ Node.val, val ≤ 10⁸, all values distinct
- val does not already exist in the BST
Edge cases to test
- Empty tree: the new node is the root
- A value smaller than every node
- A long chain
Follow-up questions
- How do you keep the tree balanced?
- Self-balancing trees rotate after inserts: AVL keeps heights within 1, red–black within a factor of 2.
- Recursive or iterative?
- The iterative walk uses O(1) space and attaches the leaf to the last node visited.
Solution
Java
public TreeNode insertIntoBST(TreeNode root, int val) {
TreeNode node = new TreeNode(val);
if (root == null) return node;
TreeNode curr = root;
while (true) {
if (val < curr.val) {
if (curr.left == null) { curr.left = node; break; }
curr = curr.left;
} else {
if (curr.right == null) { curr.right = node; break; }
curr = curr.right;
}
}
return root;
}Python
def insert_into_bst(root, val):
node = TreeNode(val)
if not root:
return node
curr = root
while True:
if val < curr.val:
if not curr.left:
curr.left = node
break
curr = curr.left
else:
if not curr.right:
curr.right = node
break
curr = curr.right
return rootC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
TreeNode *insertIntoBST(TreeNode *root, int val) {
TreeNode *node = new TreeNode(val);
if (root == nullptr)
return node;
TreeNode *curr = root;
while (true) {
if (val < curr->val) {
if (curr->left == nullptr) {
curr->left = node;
break;
}
curr = curr->left;
} else {
if (curr->right == nullptr) {
curr->right = node;
break;
}
curr = curr->right;
}
}
return root;
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)