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 : 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.
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
Postorder successor of 19 is 18
Time Complexity : O(h) where h is height of given Binary Tree
Auxiliary Space : O(1) since no use of arrays, stacks, queues.
- Preorder Successor of a Node in Binary Tree
- Inorder Successor of a node in Binary Tree
- Level Order Successor of a node in Binary Tree
- Replace each node in binary tree with the sum of its inorder predecessor and successor
- Postorder predecessor of a Node in Binary Search Tree
- Find n-th node in Postorder traversal of a Binary Tree
- Construct a Binary Search Tree from given postorder
- Construct a Binary Tree from Postorder and Inorder
- Postorder traversal of Binary Tree without recursion and without stack
- Construct Full Binary Tree from given preorder and postorder traversals
- Kth ancestor of a node in binary tree | Set 2
- K-th ancestor of a node in Binary Tree
- Get Level of a node in a Binary Tree
- K-th ancestor of a node in Binary Tree | Set 3
- Search a node in Binary 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 firstname.lastname@example.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.