 GFG App
Open App Browser
Continue

# K-th ancestor of a node in Binary Tree

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. Recommended Practice

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++

 `/* C++ program to calculate Kth ancestor of given node */` `#include ` `#include ` `using` `namespace` `std;` ` `  `// A Binary Tree Node` `struct` `Node` `{` `    ``int` `data;` `    ``struct` `Node *left, *right;` `};`   `// function to generate array of ancestors` `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 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 ancestor` `int` `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 node` `Node* newNode(``int` `data)` `{` `    ``Node *temp = ``new` `Node;` `    ``temp->data = data;` `    ``temp->left = temp->right = NULL;` `    ``return` `temp;` `}` ` `  `// Driver program to test above functions` `int` `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<

## Java

 `/* Java program to calculate Kth ancestor of given node */` `import` `java.util.*; ` `class` `GfG {` `// A Binary Tree Node ` `static` `class` `Node ` `{ ` `    ``int` `data; ` `    ``Node left, right; ` `}`   `// function to generate array of ancestors ` `static` `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 q = ``new` `LinkedList (); ` `    ``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 ancestor ` `static` `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 node ` `static` `Node newNode(``int` `data) ` `{ ` `    ``Node temp = ``new` `Node(); ` `    ``temp.data = data; ` `    ``temp.left = ``null``;` `    ``temp.right = ``null``; ` `    ``return` `temp; ` `} `   `// Driver program to test above functions ` `public` `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

 `"""Python3 program to calculate Kth ancestor` `   ``of given node """`   `# A Binary Tree Node ` `# Utility function to create a new tree node ` `class` `newNode: `   `    ``# Constructor to create a newNode ` `    ``def` `__init__(``self``, data): ` `        ``self``.data ``=` `data ` `        ``self``.left ``=` `None` `        ``self``.right ``=` `None`   `# function to generate array of ancestors ` `def` `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 ancestor ` `def` `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 Code` `if` `__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#

 `/* C# program to calculate Kth ancestor of given node */` `using` `System;` `using` `System.Collections.Generic;`   `class` `GfG ` `{` `    `  `// A Binary Tree Node ` `public` `class` `Node ` `{ ` `    ``public` `int` `data; ` `    ``public` `Node left, right; ` `}`   `// function to generate array of ancestors ` `static` `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 q = ``new` `LinkedList (); ` `    ``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 ancestor ` `static` `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 node ` `static` `Node newNode(``int` `data) ` `{ ` `    ``Node temp = ``new` `Node(); ` `    ``temp.data = data; ` `    ``temp.left = ``null``;` `    ``temp.right = ``null``; ` `    ``return` `temp; ` `} `   `// Driver program to test above functions ` `public` `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`

## Javascript

 ``

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.```

Implementation:

## C++14

 `// C++ program for finding ` `// kth ancestor of a particular node` `#include` `using` `namespace` `std;`   `// Structure for a node` `struct` `node{` `  ``int` `data;` `  ``struct` `node *left, *right;` `  ``node(``int` `x)` `  ``{` `    ``data = x;` `    ``left = right = NULL;` `  ``}` `};`   `// Program to find kth ancestor` `bool` `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<<``"["``<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<<``"["``<data<<``"] "``;` `        ``return` `false``;` `      ``}` `      ``k = k-1;` `      ``return` `true``;` `    ``}` `  ``}` `}`   `// Driver Code` `int` `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 "``<

## Java

 `// Java program for finding ` `// kth ancestor of a particular node` `import` `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 code` `public` `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

 `# Python3 program for finding ` `# kth ancestor of a particular node` ` `  `# Structure for a node` `class` `node:` `    `  `    ``def` `__init__(``self``, data):` `        `  `        ``self``.left ``=` `None` `        ``self``.right ``=` `None` `        ``self``.data ``=` `data`   `# Program to find kth ancestor` `def` `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 code` `if` `__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#

 `// C# program for finding ` `// kth ancestor of a particular node` `using` `System;`     `// Structure for a node` `public` `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 ancestor` `static` `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 code` `static` `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`

## Javascript

 ``

Output

` is the 1th ancestor of `

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3: Iterative Approach

`The basic idea behind the iterative approach is to traverse the binary tree from the root node and keep track of the path from the root to the target node using a stack. Once we find the target node, we pop elements from the stack and add their values to a vector until we reach the kth ancestor or the stack becomes empty`

Follow the Steps below to implement the above idea:

1. Initialize a stack to keep track of the path from the root to the target node, and a vector to store the ancestors.
2. Traverse the binary tree from the root node using a while loop.
3. If the current node is not NULL, push it onto the stack and move to its left child.
4. If the current node is NULL, pop the top element from the stack. If the top element is the target node, break out of the loop. Otherwise, move to its right child.
6. Pop elements from the stack and add their values to the ancestors vector until we reach the kth ancestor or the stack becomes empty.
7. If the stack becomes empty before we reach the kth ancestor, return -1.
8. Return the value of the kth ancestor

Below is the implementation of the above approach:

## C++

 `// C++ code to implement the iterative approach ` `#include ` `#include ` `#include ` `using` `namespace` `std;`   `// Definition for a binary tree node.` `struct` `TreeNode {` `    ``int` `val;` `    ``TreeNode* left;` `    ``TreeNode* right;` `    ``TreeNode(``int` `x) : val(x), left(NULL), right(NULL) {}` `};`   `// Function to find the kth ancestor of the given node using iterative approach` `int` `kthAncestor(TreeNode* root, ``int` `node, ``int` `k) {` `    ``// Initialize a stack to keep track of the path from the root to the target node` `    ``stack s;` `    ``vector<``int``> ancestors;` `    ``bool` `found = ``false``;`   `    ``// Traverse the binary tree from the root node` `    ``while` `(root != NULL || !s.empty()) {` `        ``// If the current node is not NULL, push it onto the stack and move to its left child` `        ``if` `(root != NULL) {` `            ``s.push(root);` `            ``root = root->left;` `        ``}` `        ``// If the current node is NULL, pop the top element from the stack` `        ``// If the top element is the target node, break out of the loop` `        ``// Otherwise, move to its right child` `        ``else` `{` `            ``TreeNode* temp = s.top();` `            ``s.pop();` `            ``if` `(temp->val == node) {` `                ``found = ``true``;` `                ``break``;` `            ``}` `            ``if` `(temp->right != NULL) {` `                ``root = temp->right;` `            ``}` `        ``}` `    ``}`   `    ``// If the target node is not found, return -1` `    ``if` `(!found) {` `        ``return` `-1;` `    ``}`   `    ``// Pop elements from the stack and add their values to the ancestors vector` `    ``// until we reach the kth ancestor or the stack becomes empty` `    ``while` `(!s.empty() && k > 0) {` `        ``TreeNode* temp = s.top();` `        ``s.pop();` `        ``ancestors.push_back(temp->val);` `        ``k--;` `    ``}`   `    ``// If the stack becomes empty before we reach the kth ancestor, return -1` `    ``if` `(k > 0) {` `        ``return` `-1;` `    ``}`   `    ``// Return the value of the kth ancestor` `    ``return` `ancestors.back();` `}`   `// Driver code ` `int` `main() {` `    ``/*` `        ``Example tree:` `               ``1` `             ``/   \` `            ``2     3` `           ``/ \` `          ``4   5` `    ``*/` `    ``TreeNode* root = ``new` `TreeNode(1);` `    ``root->left = ``new` `TreeNode(2);` `    ``root->right = ``new` `TreeNode(3);` `    ``root->left->left = ``new` `TreeNode(4);` `    ``root->left->right = ``new` `TreeNode(5);`   `    ``int` `node = 4;` `    ``int` `k = 2;`   `    ``int` `kthAncestorVal = kthAncestor(root, node, k);`   `    ``cout << ``"The "` `<< k << ``"th ancestor of node "` `<< node << ``" is "` `<< kthAncestorVal << endl;`   `    ``return` `0;` `}` `// This code is contributed by Veerendra_Singh_Rajpoot`

## Java

 `import` `java.util.*;`   `// Definition for a binary tree node` `class` `TreeNode {` `    ``int` `val;` `    ``TreeNode left;` `    ``TreeNode right;`   `    ``TreeNode(``int` `x)` `    ``{` `        ``val = x;` `        ``left = ``null``;` `        ``right = ``null``;` `    ``}` `}`   `class` `Main {` `    ``// Function to find the kth ancestor of` `    ``// the given node using iterative approach` `    ``static` `int` `kthAncestor(TreeNode root, TreeNode node,` `                           ``int` `k)` `    ``{` `        ``// Initialize a stack to keep track of` `        ``// the path from the root to the target node` `        ``Stack s = ``new` `Stack<>();` `        ``List ancestors = ``new` `ArrayList<>();` `        ``boolean` `found = ``false``;`   `        ``// Traverse the binary tree from the root node` `        ``while` `(root != ``null` `|| !s.empty()) {` `            ``// If the current node is not null,` `            ``// push it onto the stack and move to its left` `            ``// child` `            ``if` `(root != ``null``) {` `                ``s.push(root);` `                ``root = root.left;` `            ``}` `            ``// If the current node is null,` `            ``// pop the top element from the stack` `            ``else` `{` `                ``TreeNode temp = s.pop();`   `                ``// If the top element is the target node,` `                ``// break out of the loop` `                ``if` `(temp.val == node.val) {` `                    ``found = ``true``;` `                    ``break``;` `                ``}`   `                ``// Otherwise, move to its right child` `                ``if` `(temp.right != ``null``) {` `                    ``root = temp.right;` `                ``}` `            ``}` `        ``}`   `        ``// If the target node is not found, return -1` `        ``if` `(!found) {` `            ``return` `-``1``;` `        ``}`   `        ``// Pop elements from the stack and add their values` `        ``// to the ancestors list until we reach the kth` `        ``// ancestor or the stack becomes empty` `        ``while` `(!s.empty() && k > ``0``) {` `            ``TreeNode temp = s.pop();` `            ``ancestors.add(temp.val);` `            ``k--;` `        ``}`   `        ``// If the stack becomes empty before we reach the` `        ``// kth ancestor, return -1` `        ``if` `(k > ``0``) {` `            ``return` `-``1``;` `        ``}`   `        ``// Return the value of the kth ancestor` `        ``return` `ancestors.get(ancestors.size() - ``1``);` `    ``}`   `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``/* Example tree:` `               ``1` `             ``/   \` `            ``2     3` `           ``/ \` `          ``4   5 */` `        ``TreeNode root = ``new` `TreeNode(``1``);` `        ``root.left = ``new` `TreeNode(``2``);` `        ``root.right = ``new` `TreeNode(``3``);` `        ``root.left.left = ``new` `TreeNode(``4``);` `        ``root.left.right = ``new` `TreeNode(``5``);`   `        ``TreeNode node = root.left.left;` `        ``int` `k = ``2``;`   `        ``int` `kthAncestorVal = kthAncestor(root, node, k);`   `        ``System.out.println(` `            ``"The "` `+ k + ``"th ancestor of node "` `+ node.val` `            ``+ ``" is "` `+ kthAncestorVal);` `    ``}` `}`

## Python3

 `# Python code to implement the iterative approach `   `# Definition for a binary tree node.` `class` `TreeNode:` `    ``def` `__init__(``self``, x):` `        ``self``.val ``=` `x` `        ``self``.left ``=` `None` `        ``self``.right ``=` `None`   `# Function to find the kth ancestor of ` `# the given node using iterative approach` `def` `kthAncestor(root, node, k):` `  `  `    ``# Initialize a stack to keep track of ` `    ``# the path from the root to the target node` `    ``s ``=` `[]` `    ``ancestors ``=` `[]` `    ``found ``=` `False`   `    ``# Traverse the binary tree from the root node` `    ``while` `root ``or` `s:` `        ``# If the current node is not None, push it onto the stack and move to its left child` `        ``if` `root:` `            ``s.append(root)` `            ``root ``=` `root.left` `        ``# If the current node is None, pop the top element from the stack` `        ``# If the top element is the target node, break out of the loop` `        ``# Otherwise, move to its right child` `        ``else``:` `            ``temp ``=` `s.pop()` `            ``if` `temp.val ``=``=` `node:` `                ``found ``=` `True` `                ``break` `            ``if` `temp.right:` `                ``root ``=` `temp.right`   `    ``# If the target node is not found, return -1` `    ``if` `not` `found:` `        ``return` `-``1`   `    ``# Pop elements from the stack and add their values to the ancestors vector` `    ``# until we reach the kth ancestor or the stack becomes empty` `    ``while` `s ``and` `k > ``0``:` `        ``temp ``=` `s.pop()` `        ``ancestors.append(temp.val)` `        ``k ``-``=` `1`   `    ``# If the stack becomes empty before we reach the kth ancestor, return -1` `    ``if` `k > ``0``:` `        ``return` `-``1`   `    ``# Return the value of the kth ancestor` `    ``return` `ancestors[``-``1``]`   `# Driver code ` `if` `__name__ ``=``=` `'__main__'``:` `    ``'''` `        ``Example tree:` `               ``1` `             ``/   \` `            ``2     3` `           ``/ \` `          ``4   5` `    ``'''` `    ``root ``=` `TreeNode(``1``)` `    ``root.left ``=` `TreeNode(``2``)` `    ``root.right ``=` `TreeNode(``3``)` `    ``root.left.left ``=` `TreeNode(``4``)` `    ``root.left.right ``=` `TreeNode(``5``)`   `    ``node ``=` `4` `    ``k ``=` `2`   `    ``kthAncestorVal ``=` `kthAncestor(root, node, k)`   `    ``print``(``"The"``, k, ``"th ancestor of node"``, node, ``"is"``, kthAncestorVal)`   `# This code is contributed by rishabmalhdijo`

## C#

 `using` `System;` `using` `System.Collections.Generic;`   `public` `class` `TreeNode {` `    ``public` `int` `val;` `    ``public` `TreeNode left;` `    ``public` `TreeNode right;` `    ``public` `TreeNode(``int` `x) { val = x; }` `}`   `public` `class` `MainClass {` `    ``public` `static` `int` `KthAncestor(TreeNode root,` `                                  ``TreeNode node, ``int` `k)` `    ``{` `        ``// Initialize a stack to keep track of the path from` `        ``// the root to the target node` `        ``Stack s = ``new` `Stack();` `        ``List<``int``> ancestors = ``new` `List<``int``>();` `        ``bool` `found = ``false``;`   `        ``// Traverse the binary tree from the root node` `        ``while` `(root != ``null` `|| s.Count > 0) {` `            ``// If the current node is not null, push it onto` `            ``// the stack and move to its left child` `            ``if` `(root != ``null``) {` `                ``s.Push(root);` `                ``root = root.left;` `            ``}` `            ``else` `{` `                ``// If the current node is null, pop the top` `                ``// element from the stack` `                ``TreeNode temp = s.Pop();`   `                ``// If the top element is the target node,` `                ``// break out of the loop` `                ``if` `(temp.val == node.val) {` `                    ``found = ``true``;` `                    ``break``;` `                ``}`   `                ``// Otherwise, move to its right child` `                ``if` `(temp.right != ``null``) {` `                    ``root = temp.right;` `                ``}` `            ``}` `        ``}`   `        ``// If the target node is not found, return -1` `        ``if` `(!found) {` `            ``return` `-1;` `        ``}`   `        ``// Pop elements from the stack and add their values` `        ``// to the ancestors vector until we reach the kth` `        ``// ancestor or the stack becomes empty` `        ``while` `(s.Count > 0 && k > 0) {` `            ``TreeNode temp = s.Pop();` `            ``ancestors.Add(temp.val);` `            ``k--;` `        ``}`   `        ``// If the stack becomes empty before we reach the` `        ``// kth ancestor, return -1` `        ``if` `(k > 0) {` `            ``return` `-1;` `        ``}`   `        ``// Return the value of the kth ancestor` `        ``return` `ancestors[ancestors.Count - 1];` `    ``}`   `    ``public` `static` `void` `Main()` `    ``{` `        ``/* Example tree:` `               ``1` `             ``/   \` `            ``2     3` `           ``/ \` `          ``4   5 */` `        ``TreeNode root = ``new` `TreeNode(1);` `        ``root.left = ``new` `TreeNode(2);` `        ``root.right = ``new` `TreeNode(3);` `        ``root.left.left = ``new` `TreeNode(4);` `        ``root.left.right = ``new` `TreeNode(5);`   `        ``int` `k = 2;` `        ``int` `kthAncestorVal` `            ``= KthAncestor(root, root.left.left, k);`   `        ``Console.WriteLine(` `            ``"The "` `+ k + ``"th ancestor of node "` `            ``+ root.left.left.val + ``" is "` `+ kthAncestorVal);` `    ``}` `}`

## Javascript

 `// JavaScript code to implement the iterative approach`   `// Definition for a binary tree node.` `class TreeNode {` `constructor(x) {` `this``.val = x;` `this``.left = ``null``;` `this``.right = ``null``;` `}` `}`   `// Function to find the kth ancestor of` `// the given node using iterative approach` `function` `kthAncestor(root, node, k) {`   `// Initialize a stack to keep track of ` `// the path from the root to the target node` `const s = [];` `const ancestors = [];` `let found = ``false``;`   `// Traverse the binary tree from the root node` `while` `(root || s.length > 0) {` `    ``// If the current node is not null, push it onto the stack and move to its left child` `    ``if` `(root) {` `        ``s.push(root);` `        ``root = root.left;` `    ``}` `    ``// If the current node is null, pop the top element from the stack` `    ``// If the top element is the target node, break out of the loop` `    ``// Otherwise, move to its right child` `    ``else` `{` `        ``const temp = s.pop();` `        ``if` `(temp.val === node) {` `            ``found = ``true``;` `            ``break``;` `        ``}` `        ``if` `(temp.right) {` `            ``root = temp.right;` `        ``}` `    ``}` `}`   `// If the target node is not found, return -1` `if` `(!found) {` `    ``return` `-1;` `}`   `// Pop elements from the stack and add their values to the ancestors vector` `// until we reach the kth ancestor or the stack becomes empty` `while` `(s.length > 0 && k > 0) {` `    ``const temp = s.pop();` `    ``ancestors.push(temp.val);` `    ``k--;` `}`   `// If the stack becomes empty before we reach the kth ancestor, return -1` `if` `(k > 0) {` `    ``return` `-1;` `}`   `// Return the value of the kth ancestor` `return` `ancestors[ancestors.length - 1];` `}`   `// Driver code` `/*` `Example tree:` `1` `/` `2 3` `/` `4 5` `*/` `const root = ``new` `TreeNode(1);` `root.left = ``new` `TreeNode(2);` `root.right = ``new` `TreeNode(3);` `root.left.left = ``new` `TreeNode(4);` `root.left.right = ``new` `TreeNode(5);`     `const node = 4;` `const k = 2;`   `const kthAncestorVal = kthAncestor(root, node, k);`   `console.log(`The \${k}th ancestor of node \${node} is \${kthAncestorVal}`);`   `// This code is contributed by akashish__`

Output

`The 2th ancestor of node 4 is 1`

Time Complexity: O(N) , where N is the number of nodes in the binary tree. This is because we need to traverse the entire tree in the worst case to find the target node and the ancestors.

Space Complexity: O(N) , where N is the number of nodes in the binary tree. This is because we are using a stack to keep track of the path from the root to the target node, and in the worst case, the entire path could be stored in the stack

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.

My Personal Notes arrow_drop_up