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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if the given Binary Tree have a Subtree with equal no of 1's and 0's
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- 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
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Subtree with given sum in a Binary Tree
- Duplicate subtree in Binary Tree | SET 2
- Find the largest BST subtree in a given Binary Tree | Set 1
- Find the largest Complete Subtree in a given Binary Tree
- Find the largest Perfect Subtree in a given Binary Tree
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check whether a given binary tree is skewed binary tree or not?
- Check whether a binary tree is a full binary tree or not
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Count pairs in a binary tree whose sum is equal to a given value x
- Root to leaf paths having equal lengths 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.