Preorder Successor of a Node in Binary Tree

Given a binary tree and a node in the binary tree, find preorder successor of the given node.

Examples: Consider the following binary tree
              20            
           /      \         
          10       26       
         /  \     /   \     
       4     18  24    27   
            /  \
           14   19
          /  \
         13  15
Input :  4
Output : 10
Preorder traversal of given tree is 20, 10, 4, 
18, 14, 13, 15, 19, 26, 24, 27.

Input :  19
Output : 15


A simple solution is to first store Preorder traversal of the given tree in an array then linearly search given node and print node next to it.

Time Complexity : O(n)
Auxiliary Space : O(n)

An efficient solution is based on below observations.

  1. If left child of given node exists, then the left child is preorder successor.
  2. If left child does not exist and given node is left child of its parent, then its sibling is its preorder successor.
  3. If none of above conditions are satisfied (left child does not exist and given node is not left child of its parent), then we move up using parent pointers until one of the following happens.
    • We reach root. In this case, preorder successor does not exist.
    • Current node (one of the ancestors of given node) is left child of its parent, in this case preorder successor is sibling of current node.
// CPP program to find preorder successor of
// a node in Binary Tree.
#include <iostream>
using namespace std;
  
struct Node {
    struct Node *left, *right, *parent;
    int key;
};
  
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->left = temp->right = temp->parent = NULL;
    temp->key = key;
    return temp;
}
  
Node* preorderSuccessor(Node* root, Node* n)
{
    // If left child exists, then it is preorder
    // successor.
    if (n->left)
        return n->left;
  
    // If left child does not exist, then
    // travel up (using parent pointers)
    // until we reach a node which is left
    // child of its parent.
    Node *curr = n, *parent = curr->parent;
    while (parent != NULL && parent->right == curr) {
        curr = curr->parent;
        parent = parent->parent;
    }
  
    // If we reached root, then the given
    // node has no preorder successor
    if (parent == NULL)
        return NULL;
  
    return parent->right;
}
  
int main()
{
    Node* root = newNode(20);
    root->parent = NULL;
    root->left = newNode(10);
    root->left->parent = root;
    root->left->left = newNode(4);
    root->left->left->parent = root->left;
    root->left->right = newNode(18);
    root->left->right->parent = root->left;
    root->right = newNode(26);
    root->right->parent = root;
    root->right->left = newNode(24);
    root->right->left->parent = root->right;
    root->right->right = newNode(27);
    root->right->right->parent = root->right;
    root->left->right->left = newNode(14);
    root->left->right->left->parent = root->left->right;
    root->left->right->left->left = newNode(13);
    root->left->right->left->left->parent = root->left->right->left;
    root->left->right->left->right = newNode(15);
    root->left->right->left->right->parent = root->left->right->left;
    root->left->right->right = newNode(19);
    root->left->right->right->parent = root->left->right;
  
    Node* res = preorderSuccessor(root, root->left->right->right);
  
    if (res) {
        printf("Preorder successor of %d is %d\n",
               root->left->right->right->key, res->key);
    }
    else {
        printf("Preorder successor of %d is NULL\n",
               root->left->right->right->key);
    }
  
    return 0;
}

Output:

Preorder successor of 19 is 26

Time Complexity : O(h) where h is height of given Binary Tree
Auxiliary Space : O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Practice Tags :

Recommended Posts:



2.2 Average Difficulty : 2.2/5.0
Based on 4 vote(s)






User Actions