Check if the given binary tree has a sub-tree with equal no of 1’s and 0’s | Set 2
Given a tree having every node’s value as either 0 or 1, the task is to find whether the given binary tree contains any sub-tree that has equal number of 0’s and 1’s, if such sub-tree is found then print Yes else print No.
There are two sub-trees with equal number of 1’s and 0’s.
Hence the output is “Yes”
- Update all the nodes of the tree so that they represent the sum of all the nodes in the sub-tree rooted at the current node.
- Now if some node exists whose value is half of the number of nodes in the tree rooted at the same node then it is a valid sub-tree.
- If no such node exists then print No.
Below is the implementation of the above approach: