VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-parent-of-a-node-in-the-given-binary-tree/

⇱ Find the parent of a node in the given binary tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the parent of a node in the given binary tree

Last Updated : 17 Nov, 2025

Given a Binary Tree and a node, the task is to find the parent of the given node in the tree. Return -1 if the given node is the root node.
Note: In a binary tree, a parent node of a given node is the node that is directly connected above the given node.

Examples:

Input: target = 3

👁 double-order-traversal-of-a-binary-tree-2

Output: 1
Explanation: Parent of the target node i.e. 3 is 1

Input: target = 1

👁 double-order-traversal-of-a-binary-tree-2

Output: -1
Explanation: Parent of the target node i.e. 1 is -1, since it is the root node.

Approach:

The idea is to write arecursive function that takes the current node and its parent as the arguments (root node is passed with -1 as its parent). If the current node is equal to the required node then print its parent and return, else call the function recursively for its children and the current node as the parent.

Below is the implementation of the above approach: 


Output
1

Time complexity: O(n), where n is the number of nodes, as each node is visited once.
Auxiliary Space: O(h), where h is the height of the tree, due to the recursive call stack.

Comment
Article Tags:
Article Tags: