🧠
Brute Force (Recursive Insertion)
💡 This approach uses straightforward recursion to find the correct spot to insert the new value. It is simple and intuitive, helping beginners understand BST traversal and insertion.
Intuition
Start at the root and recursively traverse left or right depending on the value until reaching a null position where the new node can be inserted.
Algorithm
- If the current node is null, create and return a new node with the value.
- If the value to insert is less than the current node's value, recursively insert into the left subtree.
- If the value to insert is greater than the current node's value, recursively insert into the right subtree.
- Return the current node after insertion to maintain tree connections.
💡 The recursion naturally finds the correct leaf position, but beginners may find it tricky to understand how the tree links are preserved on the way back up.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def insertIntoBST(root, val):
if root is None:
return TreeNode(val)
if val < root.val:
root.left = insertIntoBST(root.left, val)
else:
root.right = insertIntoBST(root.right, val)
return root
# Driver code to test
if __name__ == '__main__':
# Construct BST: 4,2,7,1,3
root = TreeNode(4)
root.left = TreeNode(2, TreeNode(1), TreeNode(3))
root.right = TreeNode(7)
val = 5
root = insertIntoBST(root, val)
# Simple inorder traversal to print tree values
def inorder(node):
return inorder(node.left) + [node.val] + inorder(node.right) if node else []
print(inorder(root))
Line Notes
if root is None:Base case: found the position to insert the new node
return TreeNode(val)Create and return a new node with the given value
if val < root.val:Decide to go left if value is smaller than current node
root.left = insertIntoBST(root.left, val)Recursively insert into left subtree and update link
else:If value is greater, go right
root.right = insertIntoBST(root.right, val)Recursively insert into right subtree and update link
return rootReturn current node to maintain tree structure on recursion unwind
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int val) { this.val = val; }
}
public class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) return new TreeNode(val);
if (val < root.val) root.left = insertIntoBST(root.left, val);
else root.right = insertIntoBST(root.right, val);
return root;
}
public static void main(String[] args) {
Solution sol = new Solution();
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
root.right = new TreeNode(7);
root = sol.insertIntoBST(root, 5);
printInorder(root);
}
static void printInorder(TreeNode node) {
if (node == null) return;
printInorder(node.left);
System.out.print(node.val + " ");
printInorder(node.right);
}
}
Line Notes
if (root == null) return new TreeNode(val);Base case: insert new node here
if (val < root.val)Traverse left if value is smaller
root.left = insertIntoBST(root.left, val);Recursively insert and update left child
else root.right = insertIntoBST(root.right, val);Otherwise, insert into right subtree
return root;Return current node to maintain tree structure
public static void main(String[] args)Driver code to test insertion and print inorder traversal
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (!root) return new TreeNode(val);
if (val < root->val) root->left = insertIntoBST(root->left, val);
else root->right = insertIntoBST(root->right, val);
return root;
}
void inorder(TreeNode* root) {
if (!root) return;
inorder(root->left);
cout << root->val << " ";
inorder(root->right);
}
int main() {
TreeNode* root = new TreeNode(4);
root->left = new TreeNode(2);
root->left->left = new TreeNode(1);
root->left->right = new TreeNode(3);
root->right = new TreeNode(7);
root = insertIntoBST(root, 5);
inorder(root);
cout << endl;
return 0;
}
Line Notes
if (!root) return new TreeNode(val);Base case: insert new node at null position
if (val < root->val)Go left if value is smaller
root->left = insertIntoBST(root->left, val);Recursively insert and update left child pointer
else root->right = insertIntoBST(root->right, val);Otherwise, insert into right subtree
return root;Return current node to maintain tree structure
int main()Driver code to test insertion and print inorder traversal
class TreeNode {
constructor(val, left=null, right=null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function insertIntoBST(root, val) {
if (root === null) return new TreeNode(val);
if (val < root.val) root.left = insertIntoBST(root.left, val);
else root.right = insertIntoBST(root.right, val);
return root;
}
// Driver code
const root = new TreeNode(4,
new TreeNode(2, new TreeNode(1), new TreeNode(3)),
new TreeNode(7)
);
const newRoot = insertIntoBST(root, 5);
function inorder(node) {
if (!node) return [];
return [...inorder(node.left), node.val, ...inorder(node.right)];
}
console.log(inorder(newRoot));
Line Notes
if (root === null) return new TreeNode(val);Base case: insert new node when null reached
if (val < root.val)Traverse left if value is smaller
root.left = insertIntoBST(root.left, val);Recursively insert and update left child
else root.right = insertIntoBST(root.right, val);Otherwise, insert into right subtree
return root;Return current node to maintain tree structure
console.log(inorder(newRoot));Print inorder traversal to verify insertion
In the worst case, the recursion depth equals the height of the tree h. For a balanced BST, h = O(log n), but for skewed trees, h = O(n).
💡 For n=1000 nodes in a balanced tree, about 10 recursive calls happen; in worst skewed case, up to 1000 calls.
Interview Verdict: Accepted
This approach is efficient enough for balanced trees and is the standard recursive solution for BST insertion.