Given a tree of n even nodes. The task is to find the maximum number of edges to be removed from the given tree to obtain forest of trees having even number of nodes. This problem is always solvable as given graph has even nodes.
Input : n = 10 Edge 1: 1 3 Edge 2: 1 6 Edge 3: 1 2 Edge 4: 3 4 Edge 5: 6 8 Edge 6: 2 7 Edge 7: 2 5 Edge 8: 4 9 Edge 9: 4 10 Output : 2 By removing 2 edges we can obtain the forest with even node tree. Dotted line shows removed edges. Any further removal of edge will not satisfy the even nodes condition.
Find a subtree with even number of nodes and remove it from rest of tree by removing the edge connecting it. After removal, we are left with tree with even node only because initially we have even number of nodes in the tree and removed subtree has also even node. Repeat the same procedure until we left with the tree that cannot be further decomposed in this manner.
To do this, the idea is to use Depth First Search to traverse the tree. Implement DFS function in such a manner that it will return number of nodes in the subtree whose root is node on which DFS is performed. If the number of nodes is even then remove the edge, else ignore.
Below is implementation of this approach:
Time Complexity: O(n).
This article is contributed by Anuj Chauhan. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Maximum edge removal from tree to make even forest
- Print even positioned nodes of even levels in level order of the given binary tree
- Count number of trees in a forest
- Size of the Largest Trees in a Forest formed by the given Graph
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Print levels with odd number of nodes and even number of nodes
- Difference between sums of odd level and even level nodes of a Binary Tree
- Print all even nodes of Binary Search Tree
- Difference between sums of odd position and even position nodes for each level of a Binary Tree
- Count the nodes in the given tree whose weight is even
- Count the nodes in the given tree whose weight is even parity
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Sink even nodes in Binary Tree
- Sum of all the child nodes with even parent values in a Binary Tree
- Sum of all the child nodes with even grandparents in a Binary Tree
- Difference between sum of even and odd valued nodes in a Binary Tree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Count of all prime weight nodes between given nodes in the given Tree