# Postorder predecessor of a Node in Binary Search Tree

Given a binary tree and a node in the binary tree, find Postorder predecessor 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
Postorder traversal of given tree is 4, 13, 15,
14, 19, 18, 10, 24, 27, 26, 20.

Input :  24
Output : 10
```

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.

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

An efficient solution is based on below observations.

1. If right child of given node exists, then the right child is postorder predecessor.
2. If right child does not exist and given node is left child of its parent, then its sibling is its postorder predecessor.
3. If none of above conditions are satisfied (left child does not exist and given node is not right child of its parent), then we move up using parent pointers until one of the following happens.
• We reach root. In this case, postorder predecessor does not exiss
• Current node (one of the ancestors of given node) is right child of its parent, in this case postorder predecessor is sibling of current node.

## C++

 `// C++ program to find postorder predecessor of ` `// a node in Binary Tree. ` `#include ` `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* postorderPredecessor(Node* root, Node* n) ` `{ ` `    ``// If right child exists, then it is postorder ` `    ``// predecessor. ` `    ``if` `(n->right) ` `        ``return` `n->right; ` ` `  `    ``// If right child does not exist, then ` `    ``// travel up (using parent pointers) ` `    ``// until we reach a node which is right ` `    ``// child of its parent. ` `    ``Node *curr = n, *parent = curr->parent; ` `    ``while` `(parent != NULL && parent->left == curr) { ` `        ``curr = curr->parent; ` `        ``parent = parent->parent; ` `    ``} ` ` `  `    ``// If we reached root, then the given ` `    ``// node has no postorder predecessor ` `    ``if` `(parent == NULL) ` `        ``return` `NULL; ` ` `  `    ``return` `parent->left; ` `} ` ` `  `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* nodeToCheck = root->left->right; ` ` `  `    ``Node* res = postorderPredecessor(root, nodeToCheck); ` ` `  `    ``if` `(res) { ` `        ``printf``(``"Postorder predecessor of %d is %d\n"``, ` `               ``nodeToCheck->key, res->key); ` `    ``} ` `    ``else` `{ ` `        ``printf``(``"Postorder predecessor of %d is NULL\n"``, ` `               ``nodeToCheck->key); ` `    ``} ` ` `  `    ``return` `0; ` `} `

## Python3

 `"""Python3 program to find postorder  ` `predecessor of given node."""` ` `  `# A Binary Tree Node  ` `# Utility function to create a  ` `# new tree node  ` `class` `newNode:  ` ` `  `    ``# Constructor to create a newNode  ` `    ``def` `__init__(``self``, data):  ` `        ``self``.key ``=` `data  ` `        ``self``.left ``=` `None` `        ``self``.right ``=` `self``.parent ``=` `None` ` `  `def` `postorderPredecessor(root, n): ` ` `  `    ``# If right child exists, then it  ` `    ``# is postorder predecessor.  ` `    ``if` `(n.right) : ` `        ``return` `n.right  ` ` `  `    ``# If right child does not exist, then  ` `    ``# travel up (using parent pointers)  ` `    ``# until we reach a node which is right  ` `    ``# child of its parent.  ` `    ``curr ``=` `n ` `    ``parent ``=` `curr.parent  ` `    ``while` `(parent !``=` `None` `and`  `           ``parent.left ``=``=` `curr): ` `        ``curr ``=` `curr.parent  ` `        ``parent ``=` `parent.parent  ` ` `  `    ``# If we reached root, then the given  ` `    ``# node has no postorder predecessor  ` `    ``if` `(parent ``=``=` `None``) : ` `        ``return` `None` ` `  `    ``return` `parent.left ` ` `  `# Driver Code ` `if` `__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  ` ` `  `    ``nodeToCheck ``=` `root.left.right  ` ` `  `    ``res ``=` `postorderPredecessor(root, nodeToCheck)  ` ` `  `    ``if` `(res) :  ` `        ``print``(``"Postorder predecessor of"``,  ` `               ``nodeToCheck.key, ``"is"``, res.key)  ` `     `  `    ``else``: ` `        ``print``(``"Postorder predecessor of"``,  ` `               ``nodeToCheck.key, ``"is None"``) ` ` `  `# This code is contributed  ` `# by SHUBHAMSINGH10 `

## Java

 `// Java program to find postorder predecessor ` `// of a node in Binary Tree. ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `static` `class` `Node  ` `{ ` `    ``Node left, right, parent; ` `    ``int` `key; ` `}; ` ` `  `static` `Node newNode(``int` `key) ` `{ ` `    ``Node temp = ``new` `Node(); ` `    ``temp.left = temp.right = temp.parent = ``null``; ` `    ``temp.key = key; ` `    ``return` `temp; ` `} ` ` `  `static` `Node postorderPredecessor(Node root, Node n) ` `{ ` `     `  `    ``// If right child exists, then it is  ` `    ``// postorder predecessor. ` `    ``if` `(n.right != ``null``) ` `        ``return` `n.right; ` ` `  `    ``// If right child does not exist, then ` `    ``// travel up (using parent pointers) ` `    ``// until we reach a node which is right ` `    ``// child of its parent. ` `    ``Node curr = n, parent = curr.parent; ` `    ``while` `(parent != ``null` `&& parent.left == curr) ` `    ``{ ` `        ``curr = curr.parent; ` `        ``parent = parent.parent; ` `    ``} ` ` `  `    ``// If we reached root, then the given ` `    ``// node has no postorder predecessor ` `    ``if` `(parent == ``null``) ` `        ``return` `null``; ` ` `  `    ``return` `parent.left; ` `} ` ` `  `// 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 nodeToCheck = root.left.right; ` ` `  `    ``Node res = postorderPredecessor(root, nodeToCheck); ` ` `  `    ``if` `(res != ``null``) ` `    ``{ ` `        ``System.out.printf(``"Postorder predecessor "` `+  ` `                          ``"of %d is %d\n"``, ` `                          ``nodeToCheck.key, res.key); ` `    ``} ` `    ``else` `    ``{ ` `        ``System.out.printf(``"Postorder predecessor "` `+  ` `                          ``"of %d is null\n"``, ` `                          ``nodeToCheck.key); ` `    ``} ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```Postorder predecessor of 18 is 19
```

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : SHUBHAMSINGH10, Rajput-Ji

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.