Given a tree having every node’s value as either 0 or 1, the task is to find the maximum size of the sub-tree in the given tree that has equal number of 0’s and 1’s, if no such sub-tree exists then print -1.
- Change all the nodes of the tree which are 0 to -1. Now the problem gets reduced to finding the maximum size of a sub-tree sum of whose nodes is 0.
- Update all the nodes of the tree so that they represent the sum of all nodes in the sub-tree rooted at the current node.
- Now find the size of the maximum sub-tree rooted at a node whose value is 0. If no such node is found then print -1
Below is the implementation of the above approach:
- Find largest subtree sum in a tree
- Find the largest BST subtree in a given Binary Tree | Set 1
- Find the largest Perfect Subtree in a given Binary Tree
- Find largest subtree having identical left and right subtrees
- Find the largest Complete Subtree in a given Binary Tree
- Check if the given Binary Tree have a Subtree with equal no of 1's and 0's
- Largest number in BST which is less than or equal to N
- Subtree of all nodes in a tree using DFS
- Even size subtree in n-ary tree
- Queries for DFS of a subtree in a tree
- Subtree with given sum in a Binary Tree
- Queries for M-th node in the DFS of subtree
- Duplicate subtree in Binary Tree | SET 2
- Sudo Placement[1.4] | Jumping the Subtree
- Check if two nodes are in same subtree of the root node
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.