Given a tree of N nodes and N-1 edges. The task is to print the DFS of the subtree of a given node for multiple queries. The DFS must include the given node as the root of the subtree.
In the above tree, if 1 is given as the node, then the DFS of subtree will be 1 2 4 6 7 5 3.
If 2 is given as the node, then the DFS of the subtree will be 2 4 6 7 5..
- Add the edges between the nodes in an adjacency list.
- Call DFS function to generate the DFS of the complete tree.
- Use a under array to store the height of the subtree under the given node including the node.
- In the DFS function, keep incrementing the size of subtree on every recursive call.
- Mark the node index in the DFS of complete using hashing.
- The DFS of a subtree of a node will always be a contiguous subarray starting from the node(say index ind) to (ind+height of subtree).
- Get the index of node which has been stored using hashing and print the nodes from original DFS till index = ind + height of subtree which has been stored in under[node].
Below is the implementation of the above approach.
The DFS of subtree 2: 2 4 6 7 5 The DFS of subtree 4: 4 6 7
Time Complexity: O( N + M ), where N is the number of nodes and M is the number of edges for pre-calculation and O(N) for queries in worst case.
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Queries for M-th node in the DFS of subtree
- Subtree of all nodes in a tree using DFS
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Count of distinct colors in a subtree of a Colored Tree with given min frequency for Q queries
- Find GCD of each subtree of a given node in an N-ary Tree for Q queries
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Queries for the number of nodes having values less than V in the subtree of a Node
- Count of nodes having odd divisors in the given subtree for Q queries
- Queries to find the Minimum Weight from a Subtree of atmost D-distant Nodes from Node X
- Find the largest BST subtree in a given Binary Tree | Set 1
- Subtree with given sum in a Binary Tree
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Even size subtree in n-ary tree
- Euler Tour | Subtree Sum using Segment Tree
- Find largest subtree sum in a tree
- Find the largest Complete Subtree in a given Binary Tree
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Number of leaf nodes in the subtree of every node of an n-ary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.