Given a Binary tree and a number N, write a program to find the N-th node in the Preorder traversal of the given Binary tree.
Prerequisite: Tree Traversal
Input: N = 4 11 / \ 21 31 / \ 41 51 Output: 51 Explanation: Preorder Traversal of given Binary Tree is 11 21 41 51 31, so 4th node will be 51. Input: N = 5 25 / \ 20 30 / \ / \ 18 22 24 32 Output: 30
The idea to solve this problem is to do preorder 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.
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.
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Find postorder traversal of BST from preorder traversal
- Check if a given array can represent Preorder Traversal of Binary Search Tree
- Modify a binary tree to get preorder traversal using right pointers only
- Find Leftmost and Rightmost node of BST from its given preorder traversal
- Construct a special tree from given preorder traversal
- Construct the full k-ary tree from its preorder traversal
- Iterative Preorder Traversal of an N-ary Tree
- Preorder Traversal of N-ary Tree Without Recursion
- Preorder Successor of a Node in Binary Tree
- Preorder predecessor of a Node in Binary Tree
- Level order traversal of Binary Tree using Morris Traversal
- Construct BST from given preorder traversal | Set 2
- Iterative Preorder Traversal
- Morris traversal for Preorder
- Print Postorder traversal from given Inorder and Preorder traversals
- Number of elements smaller than root using preorder traversal of a BST
- Find n-th node in Postorder traversal of a Binary Tree
- Find the kth node in vertical order traversal of a 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.