Given two integers **N** and **K** where N denotes the height of a binary tree, the task is to find the parent of the node with value K in a binary tree whose postorder traversal is first

natural numbers

For N = 3, the Tree will be - 7 / \ 3 6 / \ / \ 1 2 4 5

**Examples:**

Input:N = 4, K = 5Output:6Explanation:

Parent of the node 5 is 6. As shown in the tree above.Input:N = 5, K = 3Output:7Explanation:

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

or

Therefore, parents of such nodes can be found easily.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the` `// parent of the given node K in` `// a binary tree whose post-order` `// traversal is N natural numbers` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the parent` `// of the given node` `int` `findParent(` `int` `height, ` `int` `node)` `{` ` ` `int` `start = 1;` ` ` `int` `end = ` `pow` `(2, height) - 1;` ` ` `// Condition to check whether` ` ` `// the given node is a root node.` ` ` `// if it is then return -1 because` ` ` `// root node has no parent` ` ` `if` `(end == node)` ` ` `return` `-1;` ` ` `// Loop till we found` ` ` `// the given node` ` ` `while` `(node >= 1) {` ` ` `end = end - 1;` ` ` `// Finding the middle node of the` ` ` `// tree because at every level` ` ` `// tree parent is` ` ` `// divided into two halves` ` ` `int` `mid = start` ` ` `+ (end - start)` ` ` `/ 2;` ` ` `// if the node is found return` ` ` `// the parent always the child` ` ` `// nodes of every node` ` ` `// is node/2 or (node-1)` ` ` `if` `(mid == node || end == node) {` ` ` `return` `(end + 1);` ` ` `}` ` ` `// if the node to be found` ` ` `// is greater than the mid` ` ` `// search for left subtree else` ` ` `// search in right subtree` ` ` `else` `if` `(node < mid) {` ` ` `end = mid;` ` ` `}` ` ` `else` `{` ` ` `start = mid;` ` ` `}` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `height = 4;` ` ` `int` `node = 6;` ` ` `int` `k = findParent(height, node);` ` ` `cout << k;` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the` `// parent of the given node K in` `// a binary tree whose post-order` `// traversal is N natural numbers` `import` `java.util.*;` `class` `GFG{` `// Function to find the parent` `// of the given node` `static` `int` `findParent(` `int` `height, ` ` ` `int` `node)` `{` ` ` `int` `start = ` `1` `;` ` ` `int` `end = (` `int` `)Math.pow(` `2` `, height) - ` `1` `;` ` ` `// Condition to check whether` ` ` `// the given node is a root node.` ` ` `// if it is then return -1 because` ` ` `// root node has no parent` ` ` `if` `(end == node)` ` ` `return` `-` `1` `;` ` ` `// Loop till we found` ` ` `// the given node` ` ` `while` `(node >= ` `1` `) ` ` ` `{` ` ` `end = end - ` `1` `;` ` ` `// Finding the middle node of the` ` ` `// tree because at every level` ` ` `// tree parent is` ` ` `// divided into two halves` ` ` `int` `mid = start + (end - start) / ` `2` `;` ` ` `// if the node is found return` ` ` `// the parent always the child` ` ` `// nodes of every node` ` ` `// is node*/2 or (node-1)` ` ` `if` `(mid == node || end == node) ` ` ` `{` ` ` `return` `(end + ` `1` `);` ` ` `}` ` ` `// if the node to be found` ` ` `// is greater than the mid` ` ` `// search for left subtree else` ` ` `// search in right subtree` ` ` `else` `if` `(node < mid) ` ` ` `{` ` ` `end = mid;` ` ` `}` ` ` `else` ` ` `{` ` ` `start = mid;` ` ` `}` ` ` `}` ` ` `return` `-` `1` `;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `height = ` `4` `;` ` ` `int` `node = ` `6` `;` ` ` `int` `k = findParent(height, node);` ` ` `System.out.print(k);` `}` `}` `// This code is contributed by gauravrajput1` |

*chevron_right*

*filter_none*

## Python

`# Python implementation to find the` `# parent of the given node` `import` `math ` `# Function to find the parent ` `# of the given node` `def` `findParent(height, node):` ` ` `start ` `=` `1` ` ` `end ` `=` `pow` `(` `2` `, height) ` `-` `1` ` ` `# Check whether the given node ` ` ` `# is a root node.if it is then ` ` ` `# return -1 because root ` ` ` `# node has no parent` ` ` `if` `(end ` `=` `=` `node):` ` ` `return` `-` `1` ` ` `# Loop till we found ` ` ` `# the given node` ` ` `while` `(node >` `=` `1` `):` ` ` `end ` `=` `end ` `-` `1` ` ` `# Find the middle node of the ` ` ` `# tree because at every level ` ` ` `# tree parent is divided ` ` ` `# into two halves` ` ` `mid ` `=` `start ` `+` `(end ` `-` `start)` `/` `/` `2` ` ` `# if the node is found ` ` ` `# return the parent` ` ` `# always the child nodes of every` ` ` `# node is node / 2 or (node-1) ` ` ` `if` `(mid ` `=` `=` `node ` `or` `end ` `=` `=` `node):` ` ` `return` `(end ` `+` `1` `)` ` ` ` ` `# if the node to be found is greater` ` ` `# than the mid search for left` ` ` `# subtree else search in right subtree` ` ` `elif` `(node < mid):` ` ` `end ` `=` `mid` ` ` `else` `:` ` ` `start ` `=` `mid` `# Driver code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `height ` `=` `4` ` ` `node ` `=` `6` ` ` ` ` `# Function Call` ` ` `k ` `=` `findParent(height, node)` ` ` `print` `(k)` |

*chevron_right*

*filter_none*

**Output:**

7

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.

## Recommended Posts:

- 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 contribute@geeksforgeeks.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.