Inorder Successor in BST (original) (raw)

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: 3
**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.

Try It Yourselfredirect icon

Table of Content

[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.

C++ `

#include <bits/stdc++.h> using namespace std;

// Node structure class Node { public: int data;
Node* left;
Node* right;
Node(int val) { data = val; left = right = NULL; } };

// Function to find Inorder Successor int inOrderSuccessor(Node *root, Node *p) {

// Edge case: if tree or given node is NULL
if (root == NULL || p == NULL)
    return -1;

Node *suc = NULL; 

// Traverse the BST
while (root != NULL) {
    
    // If current node's value is <= target node,
    // then successor must be in right subtree
    if (root->data <= p->data)
        root = root->right;
    
    // If current node's value is greater than target,
    // this node can be a successor
    else {
        suc = root;       
        root = root->left; 
    }
}

// If no successor found
if (suc == NULL)
    return -1;

return suc->data;

} //Driver Code int main() {

/*
        20
       /   \
      8     22
     / \
    4   12
       /  \
      10   14
*/
Node* root = new Node(20);
root->left = new Node(8);
root->right = new Node(22);
root->left->left = new Node(4);
root->left->right = new Node(12);
root->left->right->left = new Node(10);
root->left->right->right = new Node(14);
Node* p = root->left;

int ans = inOrderSuccessor(root, p);

if (ans == -1)
    cout << "No successor found";
else
    cout << ans;
return 0;

}

C

#include <stdio.h> #include <stdlib.h>

// Node structure struct Node { int data; struct Node* left; struct Node* right; };

// Function to create a new Node struct Node* createNode(int val) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = val; newNode->left = newNode->right = NULL; return newNode; }

// Function to find Inorder Successor int inOrderSuccessor(struct Node* root, struct Node* p) { // Edge case: if tree or given node is NULL if (root == NULL || p == NULL) return -1;

struct Node* suc = NULL;

// Traverse the BST
while (root!= NULL) {
    // If current node's value is <= target node,
    // then successor must be in right subtree
    if (root->data <= p->data)
        root = root->right;
    // If current node's value is greater than target,
    // this node can be a successor
    else {
        suc = root;
        root = root->left;
    }
}

// If no successor found
if (suc == NULL)
    return -1;

return suc->data;

}

// Driver Code int main() { struct Node* root = createNode(20); root->left = createNode(8); root->right = createNode(22); root->left->left = createNode(4); root->left->right = createNode(12); root->left->right->left = createNode(10); root->left->right->right = createNode(14); struct Node* p = root->left;

int ans = inOrderSuccessor(root, p);

if (ans == -1)
    printf("No successor found");
else
    printf("%d", ans);
return 0;

}

Java

class Node { public int data; public Node left; public Node right;

// Constructor
public Node(int val) {
    data = val;
    left = right = null;
}

}

public class GfG { // Function to find Inorder Successor public static int inOrderSuccessor(Node root, Node p) { // Edge case: if tree or given node is NULL if (root == null || p == null) return -1;

    Node suc = null;

    // Traverse the BST
    while (root!= null) {
        // If current node's value is <= target node,
        // then successor must be in right subtree
        if (root.data <= p.data)
            root = root.right;
        // If current node's value is greater than target,
        // this node can be a successor
        else {
            suc = root;
            root = root.left;
        }
    }

    // If no successor found
    if (suc == null)
        return -1;

    return suc.data;
}

public static void main(String[] args) {
    Node root = new Node(20);
    root.left = new Node(8);
    root.right = new Node(22);
    root.left.left = new Node(4);
    root.left.right = new Node(12);
    root.left.right.left = new Node(10);
    root.left.right.right = new Node(14);
    Node p = root.left;

    int ans = inOrderSuccessor(root, p);

    if (ans == -1)
        System.out.println("No successor found");
    else
        System.out.println(ans);
}

}

Python

class Node: def init(self, val): self.data = val self.left = None self.right = None

Function to find Inorder Successor

def inOrderSuccessor(root, p): # Edge case: if tree or given node is NULL if not root or not p: return -1

suc = None

# Traverse the BST
while root:
    # If current node's value is <= target node,
    # then successor must be in right subtree
    if root.data <= p.data:
        root = root.right
    # If current node's value is greater than target,
    # this node can be a successor
    else:
        suc = root
        root = root.left

# If no successor found
if suc is None:
    return -1

return suc.data

Driver Code

if name == 'main': root = Node(20) root.left = Node(8) root.right = Node(22) root.left.left = Node(4) root.left.right = Node(12) root.left.right.left = Node(10) root.left.right.right = Node(14) p = root.left

ans = inOrderSuccessor(root, p)

if ans == -1:
    print('No successor found')
else:
    print(ans)

C#

using System;

// Node structure public class Node { public int data;
public Node left;
public Node right;

// Constructor
public Node(int val) {
    data = val;
    left = right = null;
}

}

public class Program {

// Function to find Inorder Successor 
public static int inOrderSuccessor(Node root, Node p) {
    
    // Edge case
    if (root == null || p == null)
        return -1;

    Node suc = null; 

    // Traverse the BST
    while (root != null) {
        
        if (root.data <= p.data)
            root = root.right;
        else {
            suc = root;       
            root = root.left; 
        }
    }

    if (suc == null)
        return -1;

    return suc.data;
}

// Driver Code
public static void Main() {
    /*
            20
           /   \
          8     22
         / \
        4   12
           /  \
          10   14
    */

    Node root = new Node(20);
    root.left = new Node(8);
    root.right = new Node(22);
    root.left.left = new Node(4);
    root.left.right = new Node(12);
    root.left.right.left = new Node(10);
    root.left.right.right = new Node(14);

    Node p = root.left; // Node with value 8

    int ans = inOrderSuccessor(root, p);

    if (ans == -1)
        Console.WriteLine("No successor found");
    else
        Console.WriteLine(ans);
}

}

JavaScript

// Node structure class Node { constructor(val) { this.data = val; this.left = null; this.right = null; } }

// Function to find Inorder Successor function inOrderSuccessor(root, p) { // Edge case: if tree or given node is NULL if (!root || !p) return -1;

let suc = null; 

// Traverse the BST
while (root) {
    // If current node's value is <= target node,
    // then successor must be in right subtree
    if (root.data <= p.data)
        root = root.right;
    // If current node's value is greater than target,
    // this node can be a successor
    else {
        suc = root;       
        root = root.left; 
    }
}

// If no successor found
if (suc === null)
    return -1;

return suc.data;

}

//Driver Code (() => { /* 20 /
8 22 / \ 4 12 / \
10 14 */ let root = new Node(20); root.left = new Node(8); root.right = new Node(22); root.left.left = new Node(4); root.left.right = new Node(12); root.left.right.left = new Node(10); root.left.right.right = new Node(14); let p = root.left;

let ans = inOrderSuccessor(root, p);

if (ans === -1)
    console.log("No successor found");
else
    console.log(ans);

})();

`

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