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.
- If right child of given node exists, then the right child is postorder predecessor.
- If right child does not exist and given node is left child of its parent, then its sibling is its postorder predecessor.
- 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.
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.
- Replace each node in binary tree with the sum of its inorder predecessor and successor
- Preorder predecessor of a Node in Binary Tree
- Level Order Predecessor of a node in Binary Tree
- Construct a Binary Search Tree from given postorder
- Postorder successor of a Node in Binary Tree
- Find n-th node in Postorder traversal of a Binary Tree
- Find parent of given node in a Binary Tree with given postorder traversal
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Construct Full Binary Tree from given preorder and postorder traversals
- Construct a Binary Tree from Postorder and Inorder
- Postorder traversal of Binary Tree without recursion and without stack
- Binary Tree to Binary Search Tree Conversion
- Minimum swap required to convert binary tree to binary search tree
- Binary Tree to Binary Search Tree Conversion using STL set
- Difference between Binary Tree and Binary Search Tree
- Inorder predecessor and successor for a given key in BST
- Tree Traversals (Inorder, Preorder and Postorder)
- Check if given Preorder, Inorder and Postorder traversals are of same tree
- Check if given Preorder, Inorder and Postorder traversals are of same tree | Set 2
- Iterative Postorder Traversal of N-ary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.