Find the parent of a node in the given binary tree

13 Aug, 2021
Given a tree and a node, the task is to find the parent of the given node in the tree. Print -1 if the given node is the root node.Examples: 
Input: Node = 3
     1
   /   \
  2     3
 / \
4   5
Output: 1

Input: Node = 1
     1
   /   \
  2     3
 /       \
4         5
         /
        6
Output: -1
Approach: Write a recursive 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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <iostream>using namespace std; /* A binary tree node has data, pointerto left child and a pointerto right child */struct Node {    int data;    struct Node *left, *right;    Node(int data)    {        this->data = data;        left = right = NULL;    }}; // Recursive function to find the// parent of the given nodevoid findParent(struct Node* node,                int val, int parent){    if (node == NULL)        return;     // If current node is the required node    if (node->data == val) {         // Print its parent        cout << parent;    }    else {         // Recursive calls for the children        // of the current node        // Current node is now the new parent        findParent(node->left, val, node->data);        findParent(node->right, val, node->data);    }} // Driver codeint main(){    struct Node* root = new Node(1);    root->left = new Node(2);    root->right = new Node(3);    root->left->left = new Node(4);    root->left->right = new Node(5);    int node = 3;     findParent(root, node, -1);     return 0;}
// Java implementation of the approachclass GFG{ /* A binary tree node has data, pointerto left child and a pointerto right child */static class Node{    int data;    Node left, right;    Node(int data)    {        this.data = data;        left = right = null;    }}; // Recursive function to find the// parent of the given nodestatic void findParent(Node node,                       int val, int parent){    if (node == null)        return;     // If current node is the required node    if (node.data == val)    {         // Print its parent        System.out.print(parent);    }    else    {         // Recursive calls for the children        // of the current node        // Current node is now the new parent        findParent(node.left, val, node.data);        findParent(node.right, val, node.data);    }} // Driver codepublic static void main(String []args){    Node root = new Node(1);    root.left = new Node(2);    root.right = new Node(3);    root.left.left = new Node(4);    root.left.right = new Node(5);    int node = 3;     findParent(root, node, -1);}} // This code is contributed by 29AjayKumar
# Python3 implementation of# the above approach ''' A binary tree node has data, pointerto left child and a pointerto right child '''class Node:       def __init__(self, data):               self.data = data        self.left = None        self.right = None # Recursive function to find the# parent of the given nodedef findParent(node : Node,               val : int,               parent : int) -> None:    if (node is None):        return     # If current node is    # the required node    if (node.data == val):               # Print its parent        print(parent)    else:               # Recursive calls        # for the children        # of the current node        # Current node is now        # the new parent        findParent(node.left,                   val, node.data)        findParent(node.right,                   val, node.data) # Driver codeif __name__ == "__main__":     root = Node(1)    root.left = Node(2)    root.right = Node(3)    root.left.left = Node(4)    root.left.right = Node(5)    node = 3    findParent(root, node, -1) # This code is contributed by sanjeev2552
// C# implementation of the approachusing System;     class GFG{ /* A binary tree node has data, pointerto left child and a pointerto right child */public class Node{    public int data;    public Node left, right;    public Node(int data)    {        this.data = data;        left = right = null;    }}; // Recursive function to find the// parent of the given nodestatic void findParent(Node node,                         int val, int parent){    if (node == null)        return;     // If current node is the required node    if (node.data == val)    {         // Print its parent        Console.Write(parent);    }    else    {         // Recursive calls for the children        // of the current node        // Current node is now the new parent        findParent(node.left, val, node.data);        findParent(node.right, val, node.data);    }} // Driver codepublic static void Main(String []args){    Node root = new Node(1);    root.left = new Node(2);    root.right = new Node(3);    root.left.left = new Node(4);    root.left.right = new Node(5);    int node = 3;     findParent(root, node, -1);}} // This code is contributed by Rajput-Ji
<script> // JavaScript implementation of the approach     /* A binary tree node has data, pointerto left child and a pointerto right child */class Node{    constructor(data)    {        this.data = data;        this.left = null;        this.right = null;    }}; // Recursive function to find the// parent of the given nodefunction findParent(node, val, parent){    if (node == null)        return;     // If current node is the required node    if (node.data == val)    {         // Print its parent        document.write(parent);    }    else    {         // Recursive calls for the children        // of the current node        // Current node is now the new parent        findParent(node.left, val, node.data);        findParent(node.right, val, node.data);    }} // Driver codevar root = new Node(1);root.left = new Node(2);root.right = new Node(3);root.left.left = new Node(4);root.left.right = new Node(5);var node = 3; findParent(root, node, -1);  </script>
1
 
Time Complexity: O(N).Auxiliary Space: O(N). 
29AjayKumar
Rajput-Ji
sanjeev2552
importantly
pankajsharmagfg
Binary Tree
Data Structures
Recursion
Tree
Data Structures
Recursion
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
Introduction to Data Structures
Introduction to Tree Data Structure
What is Data Structure: Types, Classifications and Applications
What is Hashing | A Complete Tutorial
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction