Given n nodes of a tree and their connections, print Subtree nodes of every node.
Subtree of a node is defined as a tree which is a child of a node. The name emphasizes that everything which is a descendant of a tree node is a tree too, and is a subset of the larger tree.
Input: N = 5 0 1 1 2 0 3 3 4 Output: Subtree of node 0 is 1 2 3 4 Subtree of node 1 is 2 Subtree of node 3 is 4 Input: N = 7 0 1 1 2 2 3 0 4 4 5 4 6 Output: Subtree of node 0 is 1 2 3 4 5 6 Subtree of node 1 is 2 3 Subtree of node 4 is 5 6
Approach: Do DFS traversal for every node and print all the nodes which are reachable from a particular node.
Explanation of below code:
- When function dfs(0, 0) is called, start = 0, dfs_order.push_back(0), visited = 1 to keep track of dfs order.
- Now, consider adjacency list (adj) as considering directional path elements connected to node 0 will be in adjacency list corresponding to node 0.
- Now, recursively call dfs function till all elements traversed of adj.
- Now, dfs(1, 2) is called, Now start = 1, dfs_order.push_back(1), visited = 1 after adj elements is traversed.
- Now adj  is traversed which contain only node 2 when adj is traversed it contains no element, it will break and end=2.
- Similarly, all nodes traversed and store dfs_order in array to find subtree of nodes.
subtree of node 0 is 1 4 7 8 5 2 6 9 3 subtree of node 1 is 4 7 8 5 subtree of node 2 is 6 9 subtree of node 4 is 7 8 subtree of node 6 is 9
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
- Print all leaf nodes of an n-ary tree using DFS
- Kth ancestor of all nodes in an N-ary tree using DFS
- Queries for M-th node in the DFS of subtree
- 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
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Level with maximum number of nodes using DFS in a N-ary tree
- Count the number of nodes at a given level in a tree using DFS
- Calculate number of nodes in all subtrees | Using DFS
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Print the path between any two nodes of a tree | DFS
- 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
- Euler Tour | Subtree Sum using Segment Tree
- Check if two nodes are in same subtree of the root node
- 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
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.
Improved By : rituraj_jain