Given a tree of N nodes and N-1 edges. Also given an integer M and a node, the task is to print the M-th node in the DFS of the subtree of a given node for multiple queries.
Note: M will not be greater than the number of nodes in the subtree of the given node.
Input: M = 3, node = 1
In the above example if 1 is given as the node, then the DFS of subtree will be 1 2 4 6 7 5 3, hence if M is 3, then the 3rd node is 4
Input: M = 4, node = 2
If 2 is given as the node, then the DFS of the subtree will be 2 4 6 7 5., hence if M is 4 then the 4th node is 7.
- Add the edges between the nodes in an adjacency list.
- Call DFS function to generate the DFS of the complete tree.
- Use an 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.
- Let index of given node in the DFS of the tree be ind, then the M-th node will be at index ind + M -1 as the DFS of a subtree of a node will always be a contiguous subarray starting from the node.
Below is the implementation of the above approach.
Time Complexity: O(1), for processing each query.
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 DFS of a subtree in a tree
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Subtree of all nodes in a tree using DFS
- Queries for the number of nodes having values less than V in the subtree of a Node
- Queries to find the Minimum Weight from a Subtree of atmost D-distant Nodes from Node X
- Find GCD of each subtree of a given node in an N-ary Tree for Q queries
- Count of distinct colors in a subtree of a Colored Tree with given min frequency for Q queries
- Count of nodes having odd divisors in the given subtree for Q queries
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Check if two nodes are in same subtree of the root node
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Node having maximum number of nodes less than its value in its subtree
- Check whether a node is leaf node or not for multiple queries
- Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
- Find the largest BST subtree in a given Binary Tree | Set 1
- 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
- Find largest subtree having identical left and right subtrees
- Subtree with given sum in a Binary 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.