K-th ancestor of a node in Binary Tree

22 Mar, 2022
Given a binary tree in which nodes are numbered from 1 to n. Given a node and a positive integer K. We have to print the K-th ancestor of the given node in the binary tree. If there does not exist any such ancestor then print -1.For example in the below given binary tree, 2nd ancestor of node 4 and 5 is 1. 3rd ancestor of node 4 will be -1.  
The idea to do this is to first traverse the binary tree and store the ancestor of each node in an array of size n. For example, suppose the array is ancestor[n]. Then at index i, ancestor[i] will store the ancestor of ith node. So, the 2nd ancestor of ith node will be ancestor[ancestor[i]] and so on. We will use this idea to calculate the kth ancestor of the given node. We can use level order traversal to populate this array of ancestors.
Below is the implementation of above idea.  
C++
Java
Python3
C#
Javascript
/* C++ program to calculate Kth ancestor of given node */#include <iostream>#include <queue>using namespace std;  // A Binary Tree Nodestruct Node{    int data;    struct Node *left, *right;}; // function to generate array of ancestorsvoid generateArray(Node *root, int ancestors[]){    // There will be no ancestor of root node    ancestors[root->data] = -1;     // level order traversal to    // generate 1st ancestor    queue<Node*> q;    q.push(root);     while(!q.empty())    {        Node* temp  = q.front();        q.pop();         if (temp->left)        {            ancestors[temp->left->data] = temp->data;            q.push(temp->left);        }         if (temp->right)        {            ancestors[temp->right->data] = temp->data;            q.push(temp->right);        }    }} // function to calculate Kth ancestorint kthAncestor(Node *root, int n, int k, int node){    // create array to store 1st ancestors    int ancestors[n+1] = {0};     // generate first ancestor array    generateArray(root,ancestors);     // variable to track record of number of    // ancestors visited    int count = 0;     while (node!=-1)    {          node = ancestors[node];        count++;         if(count==k)            break;    }     // print Kth ancestor    return node;} // Utility function to create a new tree nodeNode* newNode(int data){    Node *temp = new Node;    temp->data = data;    temp->left = temp->right = NULL;    return temp;}  // Driver program to test above functionsint main(){    // Let us create binary tree shown in above diagram    Node *root = newNode(1);    root->left = newNode(2);    root->right = newNode(3);    root->left->left = newNode(4);    root->left->right = newNode(5);      int k = 2;    int node = 5;     // print kth ancestor of given node    cout<<kthAncestor(root,5,k,node);    return 0;}
/* Java program to calculate Kth ancestor of given node */import java.util.*;class GfG {// A Binary Tree Nodestatic class Node{    int data;    Node left, right;} // function to generate array of ancestorsstatic void generateArray(Node root, int ancestors[]){    // There will be no ancestor of root node    ancestors[root.data] = -1;     // level order traversal to    // generate 1st ancestor    Queue<Node> q = new LinkedList<Node> ();    q.add(root);     while(!q.isEmpty())    {        Node temp = q.peek();        q.remove();         if (temp.left != null)        {            ancestors[temp.left.data] = temp.data;            q.add(temp.left);        }         if (temp.right != null)        {            ancestors[temp.right.data] = temp.data;            q.add(temp.right);        }    }} // function to calculate Kth ancestorstatic int kthAncestor(Node root, int n, int k, int node){    // create array to store 1st ancestors    int ancestors[] = new int[n + 1];     // generate first ancestor array    generateArray(root,ancestors);     // variable to track record of number of    // ancestors visited    int count = 0;     while (node!=-1)    {        node = ancestors[node];        count++;         if(count==k)            break;    }     // print Kth ancestor    return node;} // Utility function to create a new tree nodestatic Node newNode(int data){    Node temp = new Node();    temp.data = data;    temp.left = null;    temp.right = null;    return temp;} // Driver program to test above functionspublic static void main(String[] args){    // Let us create binary tree shown in above diagram    Node root = newNode(1);    root.left = newNode(2);    root.right = newNode(3);    root.left.left = newNode(4);    root.left.right = newNode(5);     int k = 2;    int node = 5;     // print kth ancestor of given node    System.out.println(kthAncestor(root,5,k,node));}}
"""Python3 program to calculate Kth ancestor   of given node """ # A Binary Tree Node# Utility function to create a new tree nodeclass newNode:     # Constructor to create a newNode    def __init__(self, data):        self.data = data        self.left = None        self.right = None # function to generate array of ancestorsdef generateArray(root, ancestors):     # There will be no ancestor of root node    ancestors[root.data] = -1     # level order traversal to    # generate 1st ancestor    q = []    q.append(root)     while(len(q)):        temp = q[0]        q.pop(0)         if (temp.left):            ancestors[temp.left.data] = temp.data            q.append(temp.left)             if (temp.right):            ancestors[temp.right.data] = temp.data            q.append(temp.right) # function to calculate Kth ancestordef kthAncestor(root, n, k, node):         # create array to store 1st ancestors    ancestors = [0] * (n + 1)     # generate first ancestor array    generateArray(root,ancestors)     # variable to track record of number    # of ancestors visited    count = 0     while (node != -1) :        node = ancestors[node]        count += 1        if(count == k):            break                 # print Kth ancestor    return node                         # Driver Codeif __name__ == '__main__':     # Let us create binary tree shown    # in above diagram    root = newNode(1)    root.left = newNode(2)    root.right = newNode(3)    root.left.left = newNode(4)    root.left.right = newNode(5)         k = 2    node = 5     # print kth ancestor of given node    print(kthAncestor(root, 5, k, node)) # This code is contributed by# SHUBHAMSINGH10
/* C# program to calculate Kth ancestor of given node */using System;using System.Collections.Generic; class GfG{     // A Binary Tree Nodepublic class Node{    public int data;    public Node left, right;} // function to generate array of ancestorsstatic void generateArray(Node root, int []ancestors){    // There will be no ancestor of root node    ancestors[root.data] = -1;     // level order traversal to    // generate 1st ancestor    LinkedList<Node> q = new LinkedList<Node> ();    q.AddLast(root);     while(q.Count != 0)    {        Node temp = q.First.Value;        q.RemoveFirst();         if (temp.left != null)        {            ancestors[temp.left.data] = temp.data;            q.AddLast(temp.left);        }         if (temp.right != null)        {            ancestors[temp.right.data] = temp.data;            q.AddLast(temp.right);        }    }} // function to calculate Kth ancestorstatic int kthAncestor(Node root, int n, int k, int node){    // create array to store 1st ancestors    int []ancestors = new int[n + 1];     // generate first ancestor array    generateArray(root,ancestors);     // variable to track record of number of    // ancestors visited    int count = 0;     while (node != -1)    {        node = ancestors[node];        count++;         if(count == k)            break;    }     // print Kth ancestor    return node;} // Utility function to create a new tree nodestatic Node newNode(int data){    Node temp = new Node();    temp.data = data;    temp.left = null;    temp.right = null;    return temp;} // Driver program to test above functionspublic static void Main(String[] args){    // Let us create binary tree shown in above diagram    Node root = newNode(1);    root.left = newNode(2);    root.right = newNode(3);    root.left.left = newNode(4);    root.left.right = newNode(5);     int k = 2;    int node = 5;     // print kth ancestor of given node    Console.WriteLine(kthAncestor(root,5,k,node));}} // This code has been contributed by 29AjayKumar
<script> /* JavaScript program to calculateKth ancestor of given node */ // A Binary Tree Nodeclass Node{  constructor()  {    this.data = 0;    this.left = null;    this.right = null;  }} // function to generate array of ancestorsfunction generateArray(root, ancestors){    // There will be no ancestor of root node    ancestors[root.data] = -1;     // level order traversal to    // generate 1st ancestor    var q = [];    q.push(root);     while(q.length != 0)    {        var temp = q[0];        q.shift();         if (temp.left != null)        {            ancestors[temp.left.data] = temp.data;            q.push(temp.left);        }         if (temp.right != null)        {            ancestors[temp.right.data] = temp.data;            q.push(temp.right);        }    }} // function to calculate Kth ancestorfunction kthAncestor(root, n, k, node){    // create array to store 1st ancestors    var ancestors = Array(n+1).fill(0);     // generate first ancestor array    generateArray(root,ancestors);     // variable to track record of number of    // ancestors visited    var count = 0;     while (node != -1)    {        node = ancestors[node];        count++;         if(count == k)            break;    }     // print Kth ancestor    return node;} // Utility function to create a new tree nodefunction newNode(data){    var temp = new Node();    temp.data = data;    temp.left = null;    temp.right = null;    return temp;} // Driver program to test above functions// Let us create binary tree shown in above diagramvar root = newNode(1);root.left = newNode(2);root.right = newNode(3);root.left.left = newNode(4);root.left.right = newNode(5);var k = 2;var node = 5;// print kth ancestor of given nodedocument.write(kthAncestor(root,5,k,node));  </script>
Output: 
1
Time Complexity : O( n ) Auxiliary Space : O( n )
Method 2: In this method first we will get an element whose ancestor has to be searched and from that node, we will decrement count one by one till we reach that ancestor node. for example – 
consider the tree given below:- 
         (1)
        /    \
      (4)   (2)
     /    \      \
   (3)  (7)    (6)
              \
              (8)
Then check if k=0 if yes then return that element as an ancestor else climb a level up and reduce k (k = k-1).Initially k = 2 First we search for 8 then, at 8 => check if(k == 0) but k = 2 so k = k-1 => k = 2-1 = 1 and climb a level up i.e. at 7 at 7 => check if(k == 0) but k = 1 so k = k-1 => k = 1-1 = 0 and climb a level up i.e. at 4 at 4 => check if(k == 0) yes k = 0 return this node as ancestor.
C++14
Java
Python3
C#
Javascript
// C++ program for finding// kth ancestor of a particular node#include<bits/stdc++.h>using namespace std; // Structure for a nodestruct node{  int data;  struct node *left, *right;  node(int x)  {    data = x;    left = right = NULL;  }}; // Program to find kth ancestorbool ancestor(struct node* root, int item, int &k){  if(root == NULL)    return false;     // Element whose ancestor is to be searched  if(root->data == item)  {    //reduce count by 1    k = k-1;    return true;  }  else  {         // Checking in left side    bool flag = ancestor(root->left,item,k);    if(flag)    {      if(k == 0)      {                 // If count = 0 i.e. element is found        cout<<"["<<root->data<<"] ";        return false;      }             // if count !=0 i.e. this is not the      // ancestor we are searching for      // so decrement count      k = k-1;      return true;    }     // Similarly Checking in right side    bool flag2 = ancestor(root->right,item,k);    if(flag2)    {      if(k == 0)      {        cout<<"["<<root->data<<"] ";        return false;      }      k = k-1;      return true;    }  }} // Driver Codeint main(){  struct node* root = new node(1);  root->left = new node(4);  root->left->right = new node(7);  root->left->left = new node(3);  root->left->right->left = new node(8);  root->right = new node(2);  root->right->right = new node(6);   int item,k;  item = 3;  k = 1;  int loc = k;  bool flag =  ancestor(root,item,k);  if(flag)       cout<<"Ancestor doesn't exist\n";  else    cout<<"is the "<<loc<<"th ancestor of ["<<                                   item<<"]"<<endl;  return 0;} // This code is contributed by Sanjeev Yadav.
// Java program for finding// kth ancestor of a particular nodeimport java.io.*; class Node{    int data;    Node left, right;         Node(int x)    {        this.data = x;        this.left = this.right = null;    }} class GFG{     static int k = 1; static boolean ancestor(Node root, int item){    if (root == null)        return false;         // Element whose ancestor is to be searched    if (root.data == item)    {                 // Reduce count by 1        k = k-1;        return true;    }    else    {             // Checking in left side        boolean flag = ancestor(root.left, item);        if (flag)        {            if (k == 0)            {                             // If count = 0 i.e. element is found                System.out.print("[" + root.data + "] ");                return false;            }                     // If count !=0 i.e. this is not the            // ancestor we are searching for            // so decrement count            k = k - 1;            return true;        }             // Similarly Checking in right side        boolean flag2 = ancestor(root.right, item);        if (flag2)        {            if (k == 0)            {                System.out.print("[" + root.data + "] ");                return false;            }            k = k - 1;            return true;        }    }    return false;} // Driver codepublic static void main(String[] args){    Node root = new Node(1);    root.left = new Node(4);    root.left.right = new Node(7);    root.left.left = new Node(3);    root.left.right.left = new Node(8);    root.right = new Node(2);    root.right.right = new Node(6);         int item = 3;    int loc = k;    boolean flag = ancestor(root, item);         if (flag)        System.out.println("Ancestor doesn't exist");    else        System.out.println("is the " + (loc) +                           "th ancestor of [" +                           (item) + "]");}} // This code is contributed by avanitrachhadiya2155
# Python3 program for finding# kth ancestor of a particular node  # Structure for a nodeclass node:         def __init__(self, data):                 self.left = None        self.right = None        self.data = data # Program to find kth ancestordef ancestor(root, item):         global k         if (root == None):        return False         # Element whose ancestor is    # to be searched    if (root.data == item):                 # Reduce count by 1        k = k - 1        return True       else:          # Checking in left side        flag = ancestor(root.left, item);                 if (flag):            if (k == 0):                                 # If count = 0 i.e. element is found                print("[" + str(root.data) + "]", end = ' ')                return False                     # If count !=0 i.e. this is not the            # ancestor we are searching for            # so decrement count            k = k - 1            return True             # Similarly Checking in right side        flag2 = ancestor(root.right, item)                 if (flag2):            if (k == 0):                print("[" + str(root.data) + "]")                return False                   k = k - 1            return True # Driver codeif __name__=="__main__":         root = node(1)    root.left = node(4)    root.left.right = node(7)    root.left.left = node(3)    root.left.right.left = node(8)    root.right = node(2)    root.right.right = node(6)         item = 3    k = 1    loc = k    flag = ancestor(root, item)         if (flag):        print("Ancestor doesn't exist")    else:        print("is the " + str(loc) +              "th ancestor of [" + str(item) + "]")      # This code is contributed by rutvik_56
// C# program for finding// kth ancestor of a particular nodeusing System;  // Structure for a nodepublic class Node{    public int data;    public Node left, right;          public Node(int x)    {        this.data = x;        this.left = this.right = null;    }} class GFG{     static int k = 1; // Program to find kth ancestorstatic bool ancestor(Node root, int item){    if (root == null)        return false;          // Element whose ancestor is    // to be searched    if (root.data == item)    {                 // Reduce count by 1        k = k - 1;        return true;    }    else    {                 // Checking in left side        bool flag = ancestor(root.left, item);        if (flag)        {            if (k == 0)            {                                 // If count = 0 i.e. element is found                Console.Write("[" + root.data + "] ");                return false;            }                      // If count !=0 i.e. this is not the            // ancestor we are searching for            // so decrement count            k = k - 1;            return true;        }              // Similarly Checking in right side        bool flag2 = ancestor(root.right, item);        if (flag2)        {            if (k == 0)            {                Console.Write("[" + root.data + "] ");                return false;            }            k = k - 1;            return true;        }    }    return false;} // Driver codestatic public void Main(){    Node root = new Node(1);    root.left = new Node(4);    root.left.right = new Node(7);    root.left.left = new Node(3);    root.left.right.left = new Node(8);    root.right = new Node(2);    root.right.right = new Node(6);          int item = 3;    int loc = k;    bool flag = ancestor(root, item);          if (flag)        Console.WriteLine("Ancestor doesn't exist");    else        Console.WriteLine("is the " + (loc) +                          "th ancestor of [" +                          (item) + "]");}} // This code is contributed by patel2127
<script> class Node{    constructor(x)    {        this.data=x;        this.left = this.right = null;    }} function ancestor(root, item){    if (root == null)    {        return false;    }         if (root.data == item)    {        k = k - 1;        return true;    }         else    {        let flag = ancestor(root.left, item);        if (flag)        {            if (k == 0)            {                document.write("[" + (root.data) + "] ");                return false;            }            k = k - 1;            return true;        }        let flag2 = ancestor(root.right, item);        if (flag2)        {            if (k == 0)            {                document.write("[" + (root.data) + "] ");                return false;            }            k = k - 1;            return true;        }             }} let root = new Node(1)root.left = new Node(4)root.left.right = new Node(7)root.left.left = new Node(3)root.left.right.left = new Node(8)root.right = new Node(2)root.right.right = new Node(6)let item = 3    let k = 1   let loc = k   let flag = ancestor(root, item)          if (flag)        document.write("Ancestor doesn't exist")    else        document.write("is the " + (loc) +              "th ancestor of [" + (item) + "]")               // This code is contributed by rag2127 </script>
[4] is the 1th ancestor of [3]
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
prerna saini
SHUBHAMSINGH10
29AjayKumar
veejnas7
rutvik_56
sooda367
itsok
rag2127
avanitrachhadiya2155
patel2127
surinderdawra388
simmytarika5
cpp-queue
Tree
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Introduction to Tree Data Structure
What is Data Structure: Types, Classifications and Applications
Binary Tree | Set 3 (Types of Binary Tree)
Binary Tree | Set 2 (Properties)
Diameter of a Binary Tree
Decision Tree
Diagonal Traversal of Binary Tree
Construct Tree from given Inorder and Preorder traversals
Insertion in a Binary Tree in level order