Find the path from root to the given nodes of a tree for multiple queries
Given a tree with N vertices numbered from 0 to N – 1 (0th node is the root node). Also, given q queries containing nodes in the tree. The task is to find the path from the root node to the given node for multiple queries.
Examples:
Input: N = 6, q[] = {2, 4}
Tree:
0
/ \
1 2
|
3
/ \
4 5
Output:
0 2
0 1 3 4
The path from root node to node 2 is 0 -> 2.
The path from root node to node 4 is 0 -> 1 -> 3 -> 4.
Approach: The path from any root vertex to any vertex ‘i’ is the path from the root vertex to its parent followed by the parent itself. This can be achieved by modifying the Breadth-First-Traversal of the tree. In the path list, for each unvisited vertex, add the copy of the path of its parent to its list and then add the parent to the list.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int sz = 1e5; // Adjacency list representation // of the tree vector<int> tree[sz]; // Boolean array to mark all the // vertices which are visited bool vis[sz]; // Array of vector where ith index // stores the path from the root // node to the ith node vector<int> path[sz]; // Utility function to create an // edge between two vertices void addEdge(int a, int b) { // Add a to b's list tree[a].push_back(b); // Add b to a's list tree[b].push_back(a); } // Modified Breadth-First Function void bfs(int node) { // Create a queue of {child, parent} queue<pair<int, int> > qu; // Push root node in the front of // the queue and mark as visited qu.push({ node, -1 }); vis[node] = true; while (!qu.empty()) { pair<int, int> p = qu.front(); // Dequeue a vertex from queue qu.pop(); vis[p.first] = true; // Get all adjacent vertices of the dequeued // vertex s. If any adjacent has not // been visited then enqueue it for (int child : tree[p.first]) { if (!vis[child]) { qu.push({ child, p.first }); // Path from the root to this vertex is // the path from root to the parent // of this vertex followed by the // parent itself path[child] = path[p.first]; path[child].push_back(p.first); } } } } // Utility Function to print the // path from root to given node void displayPath(int node) { vector<int> ans = path[node]; for (int k : ans) { cout << k << " "; } cout << node << '\n'; } // Driver code int main() { // Number of vertices int n = 6; addEdge(0, 1); addEdge(0, 2); addEdge(1, 3); addEdge(3, 4); addEdge(3, 5); // Calling modified bfs function bfs(0); // Display paths from root vertex // to the given vertices displayPath(2); displayPath(4); displayPath(5); return 0; } |
0 2 0 1 3 4 0 1 3 5
Recommended Posts:
- Print path from root to all nodes in a Complete Binary Tree
- Find the maximum sum leaf to root path in a Binary Tree
- Queries to find distance between two nodes of a Binary tree
- Queries to find distance between two nodes of a Binary tree - O(logn) method
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Sum of nodes on the longest path from root to leaf node
- Print the path common to the two paths from the root to the two given nodes
- Root to leaf path with maximum distinct nodes
- GCD from root to leaf path in an N-ary tree
- Maximum XOR with given value in the path from root to given node in the tree
- Print path from root to a given node in a binary tree
- Sort the path from root to a given node in a Binary Tree
- Print the first shortest root to leaf path in a Binary Tree
- Print common nodes on path from root (or common ancestors)
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.



