VOOZH about

URL: https://www.geeksforgeeks.org/dsa/inorder-successor-in-binary-search-tree/

⇱ Inorder Successor in BST - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Inorder Successor in BST

Last Updated : 28 Apr, 2026

Given a Binary Search Tree (BST) and a reference to a node k present in the tree, find the Inorder successor of the given node. The Inorder successor of a node is the node with the smallest value greater than the value of node k in the BST.

If no such node exists, return -1.

Example:

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

👁 2056957865

Output:
Explanation: Inorder traversal : 1 2 3 Hence, the inorder successor of 2 is 3.

Input: root = [20, 8, 22, 4, 12, N, N, N, N, 10, 14], k = 8

👁 2056957866

Output: 10
Explanation: Inorder traversal: 4 8 10 12 14 20 22. Hence, the successor of 8 is 10.

[Naive Approach] - Reverse Inorder - O(n) Time

The idea is do reverse Inorder Traversal of Binary Tree and keep track of the last visited node. Whenever we find match for the current node, we return the last visited node. Please refer Inorder Suucessor in Binary Tree for details.

[Expected Approach] Using BST Property - O(h) Time O(1) Space

We follow the idea of normal BST Search. In BST search, we get closer to the key by comparing with the current node. So the last greater key visited during search is the successor. The following cases arise during the search.

  • If we reach null, then the given target does not exist, we return null
  • If current node matches the target and right child is not empty, then successor is leftmost node in right subtree.
  • If current node is greater , then it is a potential successor, we mark it as successor and proceed to left
  • If current node is smaller or equal to the target, we proceed to right.

Output
10

Time Complexity: O(h), where h = height of the BST
Space Complexity: O(1), no extra space used, purely iterative

Comment