Given a binary tree and a node in the binary tree, find Postorder successor of the given node.
Examples: Consider the following binary tree
20
/ \
10 26
/ \ / \
4 18 24 27
/ \
14 19
/ \
13 15
Postorder traversal of given tree is 4, 13, 15, 14,
19, 18, 10, 24, 27, 26, 20.
Input : Complexity Analysis:24
Output : 27
Input : 4
Output : 13
A simple solution is to first store Postorder traversal of the given tree in an array then linearly search given node and print node next to it.Â
Complexity Analysis:
- Time Complexity : O(n)Â
- Auxiliary Space : O(n)
An efficient solution is based on below observations.Â
- If given node is root then postorder successor is NULL, since root is the last node print in a postorder traversal
- If given node is right child of parent or right child of parent is NULL, then parent is postorder successor.
- If given node is left child of parent and right child of parent is not NULL, then postorder successor is the leftmost node of parent’s right subtree
Implementation:
C++
// CPP program to find postorder successor of// given node.#include <iostream>using namespace std;  struct Node {    struct Node *left, *right, *parent;    int value;};  // Utility function to create a new node with// given value.struct Node* newNode(int value){    Node* temp = new Node;    temp->left = temp->right = temp->parent = NULL;    temp->value = value;    return temp;}  Node* postorderSuccessor(Node* root, Node* n){    // Root has no successor in postorder    // traversal    if (n == root)        return NULL;      // If given node is right child of its    // parent or parent's right is empty, then     // parent is postorder successor.    Node* parent = n->parent;    if (parent->right == NULL || parent->right == n)        return parent;      // In all other cases, find the leftmost     // child in right subtree of parent.    Node* curr = parent->right;    while (curr->left != NULL)        curr = curr->left;      return curr;}  // Driver codeint main(){    struct 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;      struct Node* res = postorderSuccessor(root, root->left->right->right);    if (res)         printf("Postorder successor of %d is %d\n",               root->left->right->right->value, res->value);       else        printf("Postorder successor of %d is NULL\n",               root->left->right->right->value);         return 0;} |
Java
// Java program to find postorder successor of // given node. import java.util.*;class GfG {Â
static class Node { Â Â Â Â Node left, right, parent; Â Â Â Â int value; }Â
// Utility function to create a new node with // given value. static Node newNode(int value) { Â Â Â Â Node temp = new Node(); Â Â Â Â temp.left = null;Â Â Â Â temp.right = null;Â Â Â Â temp.parent = null; Â Â Â Â temp.value = value; Â Â Â Â return temp; } Â
static Node postorderSuccessor(Node root, Node n) {     // Root has no successor in postorder     // traversal     if (n == root)         return null; Â
    // If given node is right child of its     // parent or parent's right is empty, then     // parent is postorder successor.     Node parent = n.parent;     if (parent.right == null || parent.right == n)         return parent; Â
    // In all other cases, find the leftmost     // child in right subtree of parent.     Node curr = parent.right;     while (curr.left != null)         curr = curr.left; Â
    return curr; } Â
// Driver code public static void main(String[] args) { Â Â Â Â 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 = postorderSuccessor(root, root.left.right.right);     if (res != null)         System.out.println("Postorder successor of "+         root.left.right.right.value + " is "+ res.value);     else        System.out.println("Postorder successor of " +         root.left.right.right.value + " is NULL");} } |
Python3
""" Python3 program to find postorder successor of a node in Binary Tree."""Â
# A Binary Tree Node # Utility function to create a new tree node class newNode: Â
    # Constructor to create a new node     def __init__(self, data):         self.value = data         self.left = None        self.right = None        self.parent=NoneÂ
def postorderSuccessor(root, n) :Â
    # Root has no successor in postorder     # traversal     if (n == root):        return None         # If given node is right child of its     # parent or parent's right is empty,     # then parent is postorder successor.     parent = n.parent     if (parent.right == None or parent.right == n):        return parent          # In all other cases, find the leftmost     # child in right subtree of parent.     curr = parent.right     while (curr.left != None):        curr = curr.left          return curr     # Driver Codeif __name__ == '__main__':    root = newNode(20)     root.parent = None    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    res = postorderSuccessor(root, root.left.right.right) Â
    if (res) :         print("postorder successor of",                root.left.right.right.value,                "is", res.value)          else:        print("postorder successor of",                root.left.right.right.value,                                  "is None")Â
# This code is contributed by SHUBHAMSINGH10 |
C#
// C# program to find postorder successor of // given node.using System;Â
class GfG {Â
class Node { Â Â Â Â public Node left, right, parent; Â Â Â Â public int value; }Â
// Utility function to create // a new node with given value. static Node newNode(int value) {     Node temp = new Node();     temp.left = null;    temp.right = null;    temp.parent = null;     temp.value = value;     return temp; } Â
static Node postorderSuccessor(Node root, Node n) {     // Root has no successor in     // postorder traversal     if (n == root)         return null; Â
    // If given node is right child of its     // parent or parent's right is empty, then     // parent is postorder successor.     Node parent = n.parent;     if (parent.right == null || parent.right == n)         return parent; Â
    // In all other cases, find the leftmost     // child in right subtree of parent.     Node curr = parent.right;     while (curr.left != null)         curr = curr.left; Â
    return curr; } Â
// Driver code public static void Main(String[] args) { Â Â Â Â 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 = postorderSuccessor(root, root.left.right.right);     if (res != null)         Console.WriteLine("Postorder successor of "+         root.left.right.right.value + " is "+ res.value);     else        Console.WriteLine("Postorder successor of " +         root.left.right.right.value + " is NULL");} }Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â
// javascript program to find postorder successor of // given node.Â
class Node { Â Â Â Â constructor()Â Â Â Â {Â Â Â Â Â Â Â Â this.value = 0;Â Â Â Â Â Â Â Â this.left = null;Â Â Â Â Â Â Â Â this.right = null;Â Â Â Â Â Â Â Â this.parent = null;Â Â Â Â }}Â
// Utility function to create // a new node with given value. function newNode(value) {     var temp = new Node();     temp.left = null;    temp.right = null;    temp.parent = null;     temp.value = value;     return temp; } Â
function postorderSuccessor(root, n) {     // Root has no successor in     // postorder traversal     if (n == root)         return null; Â
    // If given node is right child of its     // parent or parent's right is empty, then     // parent is postorder successor.     var parent = n.parent;     if (parent.right == null || parent.right == n)         return parent; Â
    // In all other cases, find the leftmost     // child in right subtree of parent.     var curr = parent.right;     while (curr.left != null)         curr = curr.left; Â
    return curr; } Â
// Driver code var 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; var res = postorderSuccessor(root, root.left.right.right); if (res != null)     document.write("Postorder successor of "+     root.left.right.right.value + " is "+ res.value); else    document.write("Postorder successor of " +     root.left.right.right.value + " is NULL");Â
// This code is contributed by famously.Â
</script> |
Postorder successor of 19 is 18
Complexity Analysis:
- Time Complexity : O(h) where h is height of given Binary Tree.
- Auxiliary Space : O(1) since no use of arrays, stacks, queues.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Information to that Topic: geeksforgeeks.org/postorder-successor-of-a-node-in-binary-tree/ […]
… [Trackback]
[…] Read More on that Topic: geeksforgeeks.org/postorder-successor-of-a-node-in-binary-tree/ […]