Kth ancestor of a node in binary tree | Set 2
Last Updated :
23 Feb, 2023
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 Kth 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, the 2nd ancestor of 5 is 1. 3rd ancestor of node 5 will be -1.
We have discussed a BFS-based solution for this problem in our previous article. If you observe that solution carefully, you will see that the basic approach was to first find the node and then backtrack to the kth parent. The same thing can be done using recursive DFS without using an extra array.
The idea of using DFS is to first find the given node in the tree and then backtrack k times to reach the kth ancestor. Once we have reached the kth parent, we will simply print the node and return NULL.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *left, *right;
};
Node* temp = NULL;
Node* kthAncestorDFS(Node *root, int node , int &k)
{
if (!root)
return NULL;
if (root->data == node||
(temp = kthAncestorDFS(root->left,node,k)) ||
(temp = kthAncestorDFS(root->right,node,k)))
{
if (k > 0)
k--;
else if (k == 0)
{
cout<< "Kth ancestor is: " <<root->data;
return NULL;
}
return root;
}
}
Node* newNode( int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
int main()
{
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;
Node* parent = kthAncestorDFS(root,node,k);
if (parent)
cout << "-1" ;
return 0;
}
|
Java
public class Solution
{
static class Node
{
int data;
Node left, right;
};
static Node temp = null ;
static int k;
static Node kthAncestorDFS(Node root, int node )
{
if (root == null )
return null ;
if (root.data == node||
(temp = kthAncestorDFS(root.left,node)) != null ||
(temp = kthAncestorDFS(root.right,node)) != null )
{
if (k > 0 )
k--;
else if (k == 0 )
{
System.out.print( "Kth ancestor is: " +root.data);
return null ;
}
return root;
}
return null ;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
public static void main(String args[])
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.left.right = newNode( 5 );
k = 2 ;
int node = 5 ;
Node parent = kthAncestorDFS(root,node);
if (parent != null )
System.out.println( "-1" );
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def kthAncestorDFS(root, node, k):
if ( not root):
return None
if (root.data = = node or
(kthAncestorDFS(root.left, node, k)) or
(kthAncestorDFS(root.right, node, k))):
if (k[ 0 ] > 0 ):
k[ 0 ] - = 1
elif (k[ 0 ] = = 0 ):
print ( "Kth ancestor is:" , root.data)
return None
return root
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
parent = kthAncestorDFS(root,node,k)
if (parent):
print ( "-1" )
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node left, right;
};
static Node temp = null ;
static int k;
static Node kthAncestorDFS(Node root, int node )
{
if (root == null )
return null ;
if (root.data == node||
(temp = kthAncestorDFS(root.left,node)) != null ||
(temp = kthAncestorDFS(root.right,node)) != null )
{
if (k > 0)
k--;
else if (k == 0)
{
Console.Write( "Kth ancestor is: " +root.data);
return null ;
}
return root;
}
return null ;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
public static void Main(String []args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
k = 2;
int node = 5;
Node parent = kthAncestorDFS(root,node);
if (parent != null )
Console.WriteLine( "-1" );
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .left = null ;
this .right = null ;
}
};
var temp = null ;
var k = 0;
function kthAncestorDFS(root, node )
{
if (root == null )
return null ;
if (root.data == node||
(temp = kthAncestorDFS(root.left,node)) != null ||
(temp = kthAncestorDFS(root.right,node)) != null )
{
if (k > 0)
k--;
else if (k == 0)
{
document.write( "Kth ancestor is: " +root.data);
return null ;
}
return root;
}
return null ;
}
function newNode(data)
{
var temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
k = 2;
var node = 5;
var parent = kthAncestorDFS(root,node);
if (parent != null )
document.write( "-1" );
</script>
|
Output
Kth ancestor is: 1
Time Complexity: O(n), where n is the number of nodes in the binary tree.
Auxiliary Space: O(h) where h is the height of binary tree due to recursion call.
Share your thoughts in the comments
Please Login to comment...