Find the Kth node in the DFS traversal of a given subtree in a Tree
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
: 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++
// C++ program to find the Kth node in the // DFS traversal of the subtree of given // vertex V in a Tree #include <bits/stdc++.h> using namespace std; #define N 100005 // To store nodes int n; vector<int> tree[N]; // To store the current index of vertex in DFS int currentIdx; // To store the starting index and ending // index of vertex in the DFS traversal array vector<int> startIdx, endIdx; // To store the DFS of vertex 1 vector<int> p; // Function to add edge between two nodes void Add_edge(int u, int v) { tree[u].push_back(v); tree[v].push_back(u); } // Initialize the vectors void intisalise() { startIdx.resize(n); endIdx.resize(n); p.resize(n); } // Function to perform DFS of a vertex // 1. stores the DFS of the vertex 1 in vector p, // 2. store the start index of DFS of every vertex // 3. store the end index of DFS of every vertex void Dfs(int ch, int par) { p[currentIdx] = ch; // store staring index of node ch startIdx[ch] = currentIdx++; for (auto c : tree[ch]) { if (c != par) Dfs(c, ch); } // store ending index endIdx[ch] = currentIdx - 1; } // Function to find the Kth node in DFS of vertex V int findNode(int v, int k) { k += startIdx[v] - 1; // check if kth number exits or not if (k <= endIdx[v]) return p[k]; return -1; } // Driver code int main() { // number of nodes n = 9; // add edges 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(); // store DFS of 1st node Dfs(1, 0); int v = 3, k = 4; cout << findNode(v, k); return 0; } |
Python3
# Python3 program to find the Kth node in the # DFS traversal of the subtree of given # vertex V in a Tree N = 100005 n = 10tree = [[]for i in range(N)] # To store the current index of vertex in DFS currentIdx = 0 # To store the starting index and ending # index of vertex in the DFS traversal array startIdx = [0 for i in range(n)] endIdx = [0 for i in range(n)] # To store the DFS of vertex 1 p = [0 for i in range(n)] # Function to add edge between two nodes def Add_edge(u, v): tree[u].append(v) tree[v].append(u) # Initialize the vectors def intisalise(): pass # Function to perform DFS of a vertex # 1. stores the DFS of the vertex 1 in vector p, # 2. store the start index of DFS of every vertex # 3. store the end index of DFS of every vertex def Dfs(ch, par): global currentIdx p[currentIdx] = ch # store staring index of node ch startIdx[ch] = currentIdx currentIdx += 1 for c in tree[ch]: if (c != par): Dfs(c, ch) # store ending index endIdx[ch] = currentIdx - 1 # Function to find the Kth node in # DFS of vertex V def findNode(v, k): k += startIdx[v] - 1 # check if kth number exits or not if (k <= endIdx[v]): return p[k] return -1 # Driver code # number of nodes n = 9 # add edges 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() # store DFS of 1st node Dfs(1, 0) v, k = 3, 4 print(findNode(v, k)) # This code is contributed by mohit kumar |
8
Recommended Posts:
- Find n-th node in Preorder traversal of a Binary Tree
- Find n-th node in Postorder traversal of a Binary Tree
- Find the kth node in vertical order traversal of a Binary Tree
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Kth node in Diagonal Traversal of Binary Tree
- Find largest subtree sum in a tree
- Print cousins of a given node in Binary Tree | Single Traversal
- Find the largest BST subtree in a given Binary Tree | Set 1
- Find the largest Perfect Subtree in a given Binary Tree
- Find the largest Complete Subtree in a given Binary Tree
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Find n-th node of inorder traversal
- Find Leftmost and Rightmost node of BST from its given preorder traversal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29



