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
- Iterative Postorder Traversal of N-ary Tree
- 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
- 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
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)
- Iterative Postorder Traversal | Set 2 (Using One Stack)
- 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
- Find parent of each node in a tree for multiple queries
- Construct a Binary Tree from Postorder and Inorder
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Find Height of Binary Tree represented by Parent array
- Find right sibling of a binary tree with parent pointers
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.