Find the parent of a node in the given binary tree
Last Updated : 22 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report
Given a Binary Tree and a node, the task is to find the parent of the given node in the tree.Return -1if 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
Output: 1 Explanation: Parent of the target node i.e. 3 is 1
Input: target = 1
Output: -1 Explanation: Parent of the target node i.e. 3 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:
C++
// C++ implementation to find the parent of // a target node in a binary tree#include<iostream>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// Recursive function to find the parent // of the target nodeintfindParent(Node*root,inttarget,intparent){if(root==nullptr){return-1;}// If current node is the target, // return its parentif(root->data==target){returnparent;}// Recursively search in left subtreeintleftSearch=findParent(root->left,target,root->data);if(leftSearch!=-1){returnleftSearch;}// Recursively search in right subtreereturnfindParent(root->right,target,root->data);}intmain(){// Representation of the given tree// 1// / \ // 2 3// / \ // 4 5Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->left->right=newNode(5);inttarget=3;intparent=findParent(root,target,-1);cout<<parent<<endl;return0;}
Java
// Java implementation to find the parent of // a target node in a binary treeimportjava.util.*;classNode{intdata;Nodeleft;Noderight;Node(intx){data=x;left=null;right=null;}}// Recursive function to find the parent // of the target nodeclassGfG{staticintfindParent(Noderoot,inttarget,intparent){if(root==null){return-1;}// If current node is the target, // return its parentif(root.data==target){returnparent;}// Recursively search in left subtreeintleftSearch=findParent(root.left,target,root.data);if(leftSearch!=-1){returnleftSearch;}// Recursively search in right subtreereturnfindParent(root.right,target,root.data);}publicstaticvoidmain(String[]args){// Representation of the given tree// 1// / \// 2 3// / \// 4 5Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);inttarget=3;intparent=findParent(root,target,-1);System.out.println(parent);}}
Python
# Python implementation to find the parent of # a target node in a binary treeclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Recursive function to find the parent # of the target nodedeffind_parent(root,target,parent):ifrootisNone:return-1# If current node is the target, # return its parentifroot.data==target:returnparent# Recursively search in left subtreeleft_search=find_parent(root.left,target,root.data)ifleft_search!=-1:returnleft_search# Recursively search in right subtreereturnfind_parent(root.right,target,root.data)if__name__=="__main__":# Representation of the given tree# 1# / \# 2 3# / \# 4 5root=Node(1)root.left=Node(2)root.right=Node(3)root.left.left=Node(4)root.left.right=Node(5)target=3parent=find_parent(root,target,-1)print(parent)
C#
// C# implementation to find the parent of // a target node in a binary treeusingSystem;classNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intx){data=x;left=null;right=null;}}// Recursive function to find the parent // of the target nodeclassGfG{staticintFindParent(Noderoot,inttarget,intparent){if(root==null){return-1;}// If current node is the target, // return its parentif(root.data==target){returnparent;}// Recursively search in left subtreeintleftSearch=FindParent(root.left,target,root.data);if(leftSearch!=-1){returnleftSearch;}// Recursively search in right subtreereturnFindParent(root.right,target,root.data);}staticvoidMain(string[]args){// Representation of the given tree// 1// / \// 2 3// / \// 4 5Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);inttarget=3;intparent=FindParent(root,target,-1);Console.WriteLine(parent);}}
JavaScript
// JavaScript implementation to find the parent of // a target node in a binary treeclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Recursive function to find the parent // of the target nodefunctionfindParent(root,target,parent){if(root===null){return-1;}// If current node is the target, // return its parentif(root.data===target){returnparent;}// Recursively search in left subtreeletleftSearch=findParent(root.left,target,root.data);if(leftSearch!==-1){returnleftSearch;}// Recursively search in right subtreereturnfindParent(root.right,target,root.data);}// Representation of the given tree// 1// / \// 2 3// / \// 4 5letroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);lettarget=3;letparent=findParent(root,target,-1);console.log(parent);
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.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.