For N = 3, the Tree will be - 7 / \ 3 6 / \ / \ 1 2 4 5
Input: N = 4, K = 5
Parent of the node 5 is 6. As shown in the tree above.
Input: N = 5, K = 3
Parent of the node 3 is 7. As shown in the tree above.
Naive Approach: A simple approach is to build the tree according to the following pattern and then traverse the whole tree to find the parent of a given node.
Efficient Approach: The idea is to use a binary search to find the parent of the node. As we know the binary Tree of Height N has
nodes. Therefore, the search space for the binary search will be 1 to
Now each node has children value either
Therefore, parents of such nodes can be found easily.
Below is the implementation of the above approach:
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 n-th node in Postorder traversal of a Binary Tree
- Find postorder traversal of BST from preorder traversal
- Postorder traversal of Binary Tree without recursion and without stack
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Find the parent of a node in the given binary tree
- Iterative Postorder Traversal of N-ary Tree
- Postorder successor of a Node in Binary Tree
- Postorder predecessor of a Node in Binary Search Tree
- Print Postorder traversal from given Inorder and Preorder traversals
- Level order traversal of Binary Tree using Morris Traversal
- Construct Full Binary Tree from given preorder and postorder traversals
- Construct a Binary Search Tree from given postorder
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)
- Iterative Postorder Traversal | Set 2 (Using One Stack)
- Iterative Postorder traversal | Set 3
- Find parent of each node in a tree for multiple queries
- Find n-th node in Preorder traversal of a Binary Tree
- Find the kth node in vertical order traversal of a Binary Tree
- Find Height of Binary Tree represented by Parent array
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.
Improved By : GauravRajput1