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 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 or . Therefore, parent of such node 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 the parent of a node in the given binary tree
- Postorder traversal of Binary Tree without recursion and without stack
- Find n-th node in Preorder traversal of a Binary Tree
- Find the kth node in vertical order traversal of a Binary Tree
- Postorder successor of a Node in Binary Tree
- Postorder predecessor of a Node in Binary Search Tree
- Find parent of each node in a tree for multiple queries
- Kth node in Diagonal Traversal of Binary Tree
- Find right sibling of a binary tree with parent pointers
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- Print cousins of a given node in Binary Tree | Single Traversal
- Iterative Postorder Traversal of N-ary Tree
- Find Height of Binary Tree represented by Parent array
- Find postorder traversal of BST from preorder traversal
- Create a binary tree from post order traversal and leaf node array
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Level order traversal of Binary Tree using Morris Traversal
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Construct a Binary Tree from Postorder and Inorder
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.