Find the Kth node in the DFS traversal of a given subtree in a Tree
Last Updated :
27 Jan, 2022
Given a tree with N nodes, and two integers K and V. The task is to find the Kth node in the DFS traversal of the vertex V.
Consider the below Tree:
DFS of node number 1 is [1, 2, 3, 5, 6, 8, 7, 9, 4].
DFS of node number 3 is [3, 5, 6, 8, 7, 9]
DFS of node number 7 is [7, 9]
DFS of node number 9 is [9].
Print “-1” if the numbers in the DFS of vertex V are less than K.
Examples:
Input : Tree: Shown in above image, V = 3, K = 4
Output : 8
Input : Tree: Shown in above image, V = 7, K = 3
Output : -1
Approach : Let’s construct a vector p: to store the DFS traversal of the complete tree from vertex 1. Let tinv be the position of the vertex V in the vector p (the size of the vector p in moment we call DFS from the vertex V) and toutv be the position of the first vertex pushed to the vector after leaving the subtree of vertex V (the size of the vector p in moment when we return from DFS from the vertex V). Then it is obvious that the subtree of the vertex V lies in the interval [tinv, toutv).
So, to find the Kth node in the DFS of the subtree of node V, we will have to return the Kth node in the interval [tinv, toutv).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 100005
int n;
vector< int > tree[N];
int currentIdx;
vector< int > startIdx, endIdx;
vector< int > p;
void Add_edge( int u, int v)
{
tree[u].push_back(v);
tree[v].push_back(u);
}
void intisalise()
{
startIdx.resize(n);
endIdx.resize(n);
p.resize(n);
}
void Dfs( int ch, int par)
{
p[currentIdx] = ch;
startIdx[ch] = currentIdx++;
for ( auto c : tree[ch]) {
if (c != par)
Dfs(c, ch);
}
endIdx[ch] = currentIdx - 1;
}
int findNode( int v, int k)
{
k += startIdx[v] - 1;
if (k <= endIdx[v])
return p[k];
return -1;
}
int main()
{
n = 9;
Add_edge(1, 2);
Add_edge(1, 3);
Add_edge(1, 4);
Add_edge(3, 5);
Add_edge(3, 7);
Add_edge(5, 6);
Add_edge(5, 8);
Add_edge(7, 9);
intisalise();
Dfs(1, 0);
int v = 3, k = 4;
cout << findNode(v, k);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int N = 100005 ;
static int n;
static ArrayList<
ArrayList<Integer>> tree = new ArrayList<>();
static int currentIdx;
static int [] startIdx;
static int [] endIdx;
static int [] p;
static void Add_edge( int u, int v)
{
tree.get(u).add(v);
tree.get(v).add(u);
}
static void intisalise()
{
startIdx = new int [n + 1 ];
endIdx = new int [n + 1 ];
p = new int [n + 1 ];
}
static void Dfs( int ch, int par)
{
p[currentIdx] = ch;
startIdx[ch] = currentIdx++;
for ( int c : tree.get(ch))
{
if (c != par)
Dfs(c, ch);
}
endIdx[ch] = currentIdx - 1 ;
}
static int findNode( int v, int k)
{
k += startIdx[v] - 1 ;
if (k <= endIdx[v])
return p[k];
return - 1 ;
}
public static void main(String[] args)
{
n = 9 ;
for ( int i = 0 ; i <= n; i++)
tree.add( new ArrayList<Integer>());
Add_edge( 1 , 2 );
Add_edge( 1 , 3 );
Add_edge( 1 , 4 );
Add_edge( 3 , 5 );
Add_edge( 3 , 7 );
Add_edge( 5 , 6 );
Add_edge( 5 , 8 );
Add_edge( 7 , 9 );
intisalise();
Dfs( 1 , 0 );
int v = 3 , k = 4 ;
System.out.println(findNode(v, k));
}
}
|
Python3
N = 100005
n = 10
tree = [[] for i in range (N)]
currentIdx = 0
startIdx = [ 0 for i in range (n)]
endIdx = [ 0 for i in range (n)]
p = [ 0 for i in range (n)]
def Add_edge(u, v):
tree[u].append(v)
tree[v].append(u)
def intisalise():
pass
def Dfs(ch, par):
global currentIdx
p[currentIdx] = ch
startIdx[ch] = currentIdx
currentIdx + = 1
for c in tree[ch]:
if (c ! = par):
Dfs(c, ch)
endIdx[ch] = currentIdx - 1
def findNode(v, k):
k + = startIdx[v] - 1
if (k < = endIdx[v]):
return p[k]
return - 1
n = 9
Add_edge( 1 , 2 )
Add_edge( 1 , 3 )
Add_edge( 1 , 4 )
Add_edge( 3 , 5 )
Add_edge( 3 , 7 )
Add_edge( 5 , 6 )
Add_edge( 5 , 8 )
Add_edge( 7 , 9 )
intisalise()
Dfs( 1 , 0 )
v, k = 3 , 4
print (findNode(v, k))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static int n;
static ArrayList tree = new ArrayList();
static int currentIdx;
static int [] startIdx;
static int [] endIdx;
static int [] p;
static void Add_edge( int u, int v)
{
((ArrayList)tree[u]).Add(v);
((ArrayList)tree[v]).Add(u);
}
static void intisalise()
{
startIdx = new int [n + 1];
endIdx = new int [n + 1];
p = new int [n + 1];
}
static void Dfs( int ch, int par)
{
p[currentIdx] = ch;
startIdx[ch] = currentIdx++;
foreach ( int c in (ArrayList)tree[ch])
{
if (c != par)
Dfs(c, ch);
}
endIdx[ch] = currentIdx - 1;
}
static int findNode( int v, int k)
{
k += startIdx[v] - 1;
if (k <= endIdx[v])
return p[k];
return -1;
}
public static void Main( string [] args)
{
n = 9;
for ( int i = 0; i <= n; i++)
tree.Add( new ArrayList());
Add_edge(1, 2);
Add_edge(1, 3);
Add_edge(1, 4);
Add_edge(3, 5);
Add_edge(3, 7);
Add_edge(5, 6);
Add_edge(5, 8);
Add_edge(7, 9);
intisalise();
Dfs(1, 0);
int v = 3, k = 4;
Console.Write(findNode(v, k));
}
}
|
Javascript
<script>
let N = 100005;
let n=10;
let tree = [];
let currentIdx=0;
let startIdx;
let endIdx;
let p;
function Add_edge(u,v)
{
tree[u].push(v);
tree[v].push(u);
}
function intisalise()
{
startIdx = new Array(n + 1);
endIdx = new Array(n + 1);
p = new Array(n + 1);
for (let i=0;i<(n+1);i++)
{
startIdx[i]=0;
endIdx[i]=0;
p[i]=0;
}
}
function Dfs(ch,par)
{
p[currentIdx] = ch;
startIdx[ch] = currentIdx++;
for (let c=0;c<tree[ch].length;c++)
{
if (tree[ch] != par)
Dfs(tree[ch], ch);
}
endIdx[ch] = currentIdx - 1;
}
function findNode(v,k)
{
k += startIdx[v] - 1;
if (k <= endIdx[v])
return p[k];
return -1;
}
n = 9;
for (let i = 0; i <= n; i++)
tree.push([]);
Add_edge(1, 2);
Add_edge(1, 3);
Add_edge(1, 4);
Add_edge(3, 5);
Add_edge(3, 7);
Add_edge(5, 6);
Add_edge(5, 8);
Add_edge(7, 9);
intisalise();
Dfs(1, 0);
let v = 3, k = 4;
document.write(findNode(v, k));
</script>
|
Share your thoughts in the comments
Please Login to comment...