

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Kth node in the DFS traversal of a given subtree in a Tree in C++
<p>In this problem, we are given a tree of size N, a node of the tree V and k. Our task is <em>find the Kth node in the DFS traversal of a given subtree in a Tree</em>.</p><p>We need to find the kth node in the DFS traversal of the tree starting from vertex V.</p><p><strong>Let's take an example to understand the problem,</strong></p><p>Input :</p><p><img src="https://www.tutorialspoint.com/assets/questions/media/60979/input.jpg" class="fr-fic fr-dib" width="373" height="399"></p><p>V = 2, k = 3</p><p>Output : 4</p><p><strong>Explanation</strong> −</p><pre class="result notranslate">The series is {1, 2, 3, 5, 6, 7} The 4th element is 5.</pre><h2>Solution Approach</h2><p>A simple solution to the problem is find the DFS traversal of the node V and print the kth value from this.</p><p>For this, we perform DFS traversal of the tree and store it in a vector. In this vector, we will find values for V<sub>start</sub> and V<sub>end</sub> marking the start and end of the DFS traversal of the tree. Then find the k-th value in this range and print it if possible.</p><h2>Example</h2><p>Program to illustrate the working of our solution</p><pre class="just-code notranslate language-cpp" data-lang="cpp">#include <bits/stdc++.h> using namespace std; #define N 100005 int n; vector<int> tree[N]; int currentIdx; vector<int> startIdx,endIdx; vector<int> dfsTraversalVector; void insertEdge(int u, int v){ tree[u].push_back(v); tree[v].push_back(u); } void findDfsTraversal(int ch, int par){ dfsTraversalVector[currentIdx] = ch; startIdx[ch] = currentIdx++; for (auto c : tree[ch]) { if (c != par) findDfsTraversal(c, ch); } endIdx[ch] = currentIdx - 1; } int findKNodeDfsV(int v, int k){ k = k + (startIdx[v] - 1); if (k <= endIdx[v]) return dfsTraversalVector[k]; return -1; } int main(){ n = 9; insertEdge(5, 8); insertEdge(5, 2); insertEdge(5, 10); insertEdge(5, 3); insertEdge(2, 6); insertEdge(2, 1); insertEdge(3, 9); insertEdge(6, 1); insertEdge(9, 7); startIdx.resize(n); endIdx.resize(n); dfsTraversalVector.resize(n); findDfsTraversal(5, 0); int v = 2, k = 4; cout<<k<<"-th node in DFS traversal of node "<<v<<" is "<<findKNodeDfsV(v, k); return 0; }</pre><h2>Output</h2><pre class="result notranslate">4-th node in DFS traversal of node 2 is 1</pre>
- Related Questions & Answers
- Find the kth node in vertical order traversal of a Binary Tree in C++
- Kth node in Diagonal Traversal of Binary Tree in C++
- C++ Queries for DFS of a Subtree in a Tree
- Python Program to Find Nth Node in the Inorder Traversal of a Tree
- Program to find Kth ancestor of a tree node in Python
- Find the largest Perfect Subtree in a given Binary Tree in Python
- Find the largest Complete Subtree in a given Binary Tree in Python
- Find the largest Complete Subtree in a given Binary Tree in C++
- Find n-th node in Postorder traversal of a Binary Tree in C++
- Find n-th node in Preorder traversal of a Binary Tree in C++
- Find the largest BST subtree in a given Binary Tree - Set 1 in C++
- Find the Kth Node from the end in a given singly Linked List using C++
- Find mirror of a given node in Binary tree in C++
- Find the Number of Siblings of a Given Node in N-ary Tree using C++
- Find largest subtree sum in a tree in C++
Advertisements