LeetCode 701: Insert into a Binary Search Tree – Java Recursive Solution with Dry Run

Learn how to solve LeetCode 701 Insert into a Binary Search Tree using recursion in Java. Includes BST intuition, brute force approach, optimized recursive solution, dry run, complexity analysis, interview explanation, and common mistakes.

Krishna Shrivastava
0 views
LinkedInGithubX
0
0
LeetCode 701: Insert into a Binary Search Tree – Java Recursive Solution with Dry Run
Listen to articleAudio version
Ad
Advertisement

Introduction

LeetCode 701 – Insert into a Binary Search Tree is one of the most important Binary Search Tree problems for coding interviews.

This problem helps developers understand:

  1. BST properties
  2. Recursive traversal
  3. Tree modification
  4. Node insertion logic
  5. DFS recursion

It is frequently asked because BST insertion is a foundational concept used in:

  1. Search operations
  2. Tree balancing
  3. Database indexing
  4. Ordered data structures

Problem Link

🔗 https://leetcode.com/problems/insert-into-a-binary-search-tree/

Problem Statement

You are given:

  1. Root of a Binary Search Tree
  2. A value to insert

Return:

Root of BST after insertion

The inserted value is guaranteed to be unique.

What is a Binary Search Tree?

A BST follows this rule:

Left subtree values < Root
Right subtree values > Root

Example:

4
/ \
2 7
/ \
1 3

If we insert:

5

It becomes:

4
/ \
2 7
/ \ /
1 3 5

Key Observation

While traversing:

  1. If value is smaller → go left
  2. If value is larger → go right

Eventually:

We reach a null position

That is the insertion point.

Intuition

BST insertion behaves exactly like BST search.

We recursively move:

left or right

until we find:

null

Then create the new node there.

Brute Force Thinking

A beginner might think:

  1. Store tree in array
  2. Insert value
  3. Sort again
  4. Rebuild BST

But rebuilding the tree is unnecessary and inefficient.

BST already provides ordered traversal naturally.

Optimized Recursive Approach

Idea

At every node:

  1. If value is smaller → insert into left subtree
  2. Else → insert into right subtree

When root becomes:

null

create a new node.

Java Solution

class Solution {

public void solve(TreeNode root, TreeNode prevnode, int val) {

if(root == null) {

if(prevnode.val < val) {
prevnode.right = new TreeNode(val);
}
else {
prevnode.left = new TreeNode(val);
}

return;
}

if(root.val > val) {
solve(root.left, root, val);
}
else {
solve(root.right, root, val);
}
}

public TreeNode insertIntoBST(TreeNode root, int val) {

if(root == null) {
return new TreeNode(val);
}

TreeNode originalRoot = root;

solve(root, root, val);

return originalRoot;
}
}

Cleaner Recursive Version

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;
}
}

Why This Works

BST property guarantees:

Smaller values go left
Larger values go right

So recursion naturally finds the correct insertion position.

When recursion reaches:

null

we create the new node.

Dry Run

Input

root = [4,2,7,1,3]
val = 5

Step 1

Current node:

4

Since:

5 > 4

move right.

Step 2

Current node:

7

Since:

5 < 7

move left.

Step 3

Left child is:

null

Insert:

5

Final Tree

4
/ \
2 7
/ \ /
1 3 5

Time Complexity Analysis

Average Case

Time Complexity

O(log N)

Balanced BST height remains logarithmic.

Space Complexity

O(log N)

due to recursion stack.

Worst Case

If BST becomes skewed:

1 -> 2 -> 3 -> 4

Then:

Time Complexity

O(N)

Space Complexity

O(N)

Recursive vs Iterative

ApproachTime ComplexitySpace Complexity
RecursiveO(H)O(H)
IterativeO(H)O(1)

Where:

H = height of BST

Iterative Java Solution

class Solution {

public TreeNode insertIntoBST(TreeNode root, int val) {

if(root == null) {
return new TreeNode(val);
}

TreeNode curr = root;

while(true) {

if(val < curr.val) {

if(curr.left == null) {
curr.left = new TreeNode(val);
break;
}

curr = curr.left;
}
else {

if(curr.right == null) {
curr.right = new TreeNode(val);
break;
}

curr = curr.right;
}
}

return root;
}
}

Interview Explanation

In interviews, explain:

Binary Search Tree insertion follows BST ordering rules. We recursively traverse left or right depending on the value until we reach a null node, where the new node is inserted.

This demonstrates:

  1. BST understanding
  2. Recursive traversal
  3. Tree manipulation
  4. DFS logic

Common Mistakes

1. Forgetting to Return Root

Always return original root after insertion.

2. Breaking BST Property

Incorrect comparisons can violate BST ordering.

Correct logic:

if(val < root.val)

3. Missing Base Case

Without:

if(root == null)

recursion never stops.

4. Rebuilding Entire Tree

Insertion should modify existing BST directly.

FAQs

Q1. Why use recursion?

BST naturally divides into smaller subtrees.

Recursion simplifies traversal logic.

Q2. Can insertion be iterative?

Yes.

Using loops avoids recursion stack usage.

Q3. Why does BST insertion work efficiently?

Because BST reduces search space at every step.

Q4. Is this problem important for interviews?

Absolutely.

BST insertion is one of the most frequently asked tree concepts.

Conclusion

LeetCode 701 is an excellent BST problem for learning:

  1. Recursive tree traversal
  2. BST properties
  3. Node insertion logic
  4. DFS recursion

The core insight is:

Move left for smaller values and right for larger values until a null position is found.

Once this becomes intuitive, most BST operations become much easier to solve.

Ai Assistant Kas