Inorder Successor in BST – Java Optimized BST Solution with Dry Run

Learn how to solve the GFG Inorder Successor in BST problem using optimized BST traversal in Java. Includes brute force approach, recursive BST solution, intuition, dry run, complexity analysis, interview tips, and common mistakes.

Krishna Shrivastava
1 views
LinkedInGithubX
0
0
Inorder Successor in BST – Java Optimized BST Solution with Dry Run
Listen to articleAudio version
Ad
Advertisement

Introduction

The Inorder Successor in BST problem is one of the most important Binary Search Tree interview questions asked in coding interviews and online coding platforms like GeeksforGeeks.

This problem tests your understanding of:

  1. Binary Search Tree properties
  2. Inorder traversal
  3. Recursive searching
  4. Tree optimization techniques
  5. Successor logic in BST

Understanding this problem properly helps in solving many advanced BST questions efficiently.

Problem Link

🔗 GeeksforGeeks – Inorder Successor in BST

Problem Statement

Given:

  1. A Binary Search Tree
  2. A reference node k

Find the:

Inorder Successor

of that node.

What is Inorder Successor?

The inorder successor of a node is:

The next greater node in the inorder traversal of the BST.

Example 1

Input

root = [2,1,3]
k = 2

Inorder Traversal

1 2 3

Output

3

Example 2

Input

root = [20,8,22,4,12,null,null,null,null,10,14]
k = 8

Inorder Traversal

4 8 10 12 14 20 22

Output

10

Key BST Observation

In a BST:

Left subtree -> smaller values
Right subtree -> greater values

The inorder traversal of BST is always:

Sorted order

This property helps us optimize the search.

Intuition

Suppose:

k = 8

and current node is:

20

Since:

20 > 8

this node can potentially be the inorder successor.

But there may exist a smaller valid successor in the left subtree.

So:

  1. Store current node as possible answer
  2. Move left

Brute Force Approach

Idea

  1. Perform inorder traversal
  2. Store traversal in list
  3. Find node k
  4. Return next element

Brute Force Java Solution

class Solution {

List<Integer> inorder = new ArrayList<>();

void traverse(Node root) {

if(root == null)
return;

traverse(root.left);
inorder.add(root.data);
traverse(root.right);
}

public int inOrderSuccessor(Node root, Node k) {

traverse(root);

for(int i = 0; i < inorder.size() - 1; i++) {

if(inorder.get(i) == k.data) {
return inorder.get(i + 1);
}
}

return -1;
}
}

Brute Force Complexity

Time Complexity

O(N)

Space Complexity

O(N)

because of traversal list.

Optimized BST Approach

Instead of traversing the entire tree:

  1. Use BST properties
  2. Reduce unnecessary traversal
  3. Search efficiently

Optimized Java Solution

class Solution {

int c = -1;

int solve(Node root, Node x, int c) {

if(root == null)
return c;

if(root.data > x.data) {

c = root.data;

return solve(root.left, x, c);

} else {

return solve(root.right, x, c);
}
}

public int inOrderSuccessor(Node root, Node k) {

return solve(root, k, c);
}
}

How This Solution Works

Whenever:

root.data > k.data

the current node becomes a possible successor candidate.

But there may exist a smaller valid successor in the left subtree.

So:

  1. Store current node
  2. Move left

Why Move Right Otherwise?

If:

root.data <= k.data

then current node cannot be successor.

Move right to search for larger values.

Dry Run

Input

20
/ \
8 22
/ \
4 12
/ \
10 14

k = 8

Step 1

Current node:

20

Since:

20 > 8

possible successor:

20

Move left.

Step 2

Current node:

8

Since:

8 <= 8

Move right.

Step 3

Current node:

12

Since:

12 > 8

update successor:

12

Move left.

Step 4

Current node:

10

Since:

10 > 8

update successor:

10

Move left.

Step 5

Node becomes null.

Return:

10

Final Answer

10

Alternative Inorder Traversal Approach

Another common approach:

  1. Perform inorder traversal
  2. Track previous node
  3. When previous node becomes k, current node is successor

Alternative Recursive Solution

class Solution {

Node prev = null;
Node succ = null;

void solve(Node root, Node k) {

if(root == null || succ != null)
return;

solve(root.left, k);

if(prev != null && prev.data == k.data) {
succ = root;
}

prev = root;

solve(root.right, k);
}

public int inOrderSuccessor(Node root, Node k) {

solve(root, k);

return succ != null ? succ.data : -1;
}
}

Time Complexity Analysis

Optimized BST Solution

Best Case

O(log N)

Balanced BST.

Worst Case

O(N)

Skewed BST.

Space Complexity

Recursive Stack

O(H)

where:

H = height of tree

Interview Explanation

In interviews explain:

Whenever we encounter a node greater than k, it becomes a possible inorder successor candidate. Then we move left to search for a smaller valid successor.

This demonstrates:

  1. BST optimization understanding
  2. Recursive traversal logic
  3. Efficient searching skills

Common Mistakes

1. Traversing Entire Tree Unnecessarily

BST property already reduces search space.

2. Returning First Greater Node

Need the:

smallest greater node

not any greater node.

3. Forgetting Candidate Update

Always update candidate before moving left.

4. Confusing Floor/Ceil Logic

Successor logic is different from:

  1. Floor
  2. Ceil
  3. Predecessor

FAQs

Q1. What is inorder successor?

The next greater node in inorder traversal.

Q2. Why BST helps optimization?

BST ordering allows skipping unnecessary branches.

Q3. Can inorder successor be absent?

Yes.

If node is largest element, answer is:

-1

Q4. Can this be solved iteratively?

Yes.

Iterative solution is also common in interviews.

Related BST Problems

Practice these next:

  1. Search in BST
  2. Ceil in BST
  3. Floor in BST
  4. Validate BST
  5. Lowest Common Ancestor in BST
  6. Kth Smallest Element in BST

Conclusion

Inorder Successor in BST is a very important interview problem because it teaches:

  1. BST optimization
  2. Recursive searching
  3. Successor logic
  4. Tree traversal efficiency

The key idea is:

Whenever a node is greater than k, it becomes a possible successor candidate, but a smaller valid successor may still exist in the left subtree.

Mastering this logic makes advanced BST problems significantly easier.

Ai Assistant Kas