Given a Binary tree and a number N, write a program to find the N-th node in the Postorder traversal of the given Binary tree.
Prerequisite: Tree Traversal
Input : N = 4 11 / \ 21 31 / \ 41 51 Output : 31 Explanation: Postorder Traversal of given Binary Tree is 41 51 21 31 11, so 4th node will be 31. Input : N = 5 25 / \ 20 30 / \ / \ 18 22 24 32 Output : 32
The idea to solve this problem is to do postorder traversal of the given binary tree and keep track of the count of nodes visited while traversing the tree and print the current node when the count becomes equal to N.
Below is the implementation of the above approach:
Time Complexity: O(n), where n is the number of nodes in the 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.
- Find parent of given node in a Binary Tree with given postorder traversal
- Find postorder traversal of BST from preorder traversal
- Postorder traversal of Binary Tree without recursion and without stack
- Iterative Postorder Traversal of N-ary Tree
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Postorder successor of a Node in Binary Tree
- Postorder predecessor of a Node in Binary Search Tree
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)
- Iterative Postorder Traversal | Set 2 (Using One Stack)
- Print Postorder traversal from given Inorder and Preorder traversals
- Iterative Postorder traversal | Set 3
- Level order traversal of Binary Tree using Morris Traversal
- Construct a Binary Search Tree from given postorder
- Construct Full Binary Tree from given preorder and postorder traversals
- Construct a Binary Tree from Postorder and Inorder
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Find n-th node in Preorder traversal of a Binary Tree
- Find the kth node in vertical order traversal of a Binary Tree
- Print cousins of a given node in Binary Tree | Single Traversal
- Kth node in Diagonal Traversal of 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 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.