
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Largest Subtree having Equal No of 1's and 0's
Given a binary tree. Now we are tasked to find the largest subtree having an equal number of 1's and 0's; the tree only contains 0's and 1's.
Approach to Find the Solution
In this approach, we are going to replace all the nodes with values of 0 to -1. Doing this will make our program simpler as we now need to find the largest subtree with a sum equal to 0.
Example
C++ Code for the Above Approach
#include <iostream> using namespace std; int maxi = -1; struct node { // structure of our tree node int data; struct node *right, *left; }; struct node* newnode(int key){// To create a new node struct node* temp = new node; temp->data = key; temp->right = NULL; temp->left = NULL; return temp; } void inorder(struct node* root){ // traversing the tree(not used) if (root == NULL) return; inorder(root->left); cout << root->data << endl; inorder(root->right); } // Function to return the maximum size of // the sub-tree having an equal number of 0's and 1's int calculatingmax(struct node* root){ int a = 0, b = 0; if (root == NULL) return 0; a = calculatingmax(root->right); // right subtree a = a + 1; // including parent b = calculatingmax(root->left); // left subtree a = b + a; // number of nodes at current subtree if (root->data == 0) // if the sum of whole subtree is 0 // If the total size exceeds // the current max if (a >= maxi) maxi = a; return a; } int calc_sum(struct node* root){ // updating the values at each node if (root != NULL){ if (root->data == 0){ root->data = -1; } } int a = 0, b = 0; // If left child exists if (root->left != NULL) a = calc_sum(root->left); // If right child exists if (root->right != NULL) b = calc_sum(root->right); root->data += (a + b); return root->data; } // Driver code int main(){ struct node* root = newnode(1); root->right = newnode(0); root->right->right = newnode(1); root->right->right->right = newnode(1); root->left = newnode(0); root->left->left = newnode(1); root->left->left->left = newnode(1); root->left->right = newnode(0); root->left->right->left = newnode(1); root->left->right->left->left = newnode(1); root->left->right->right = newnode(0); root->left->right->right->left = newnode(0); root->left->right->right->left->left = newnode(1); calc_sum(root); calculatingmax(root); // cout << "h"; cout << maxi; return 0; }
Output
6
Explanation of the Above Code
In the above approach, we update all the nodes having values 0 to -1 then now we reduced our problem to finding the largest subtree having a sum equal to 0 now while this updating, we also update all the nodes with the values of full of the importance of the subtree rooted at that node and now we use a second function to calculate and check every node having value 0 and then finding the max number of nodes present in that tree.
Conclusion
In this tutorial, we solve a problem to find the Largest subtree having equal no of 1’s and 0’s. We also learned the C++ program for this problem and the complete approach (Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this tutorial helpful.
- Related Articles
- Count subarrays with equal number of 1’s and 0’s in C++
- Largest number with binary representation is m 1’s and m-1 0’s in C++
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Construct DFA of alternate 0’s and 1’s
- Sort an arrays of 0’s, 1’s and 2’s using C++
- Sort an arrays of 0’s, 1’s and 2’s using Java
- Maximum sub-matrix area having count of 1’s one more than count of 0’s in C++
- Maximum length of segments of 0’s and 1’s in C++
- Maximum sub-matrix area having count of 1's one more than count of 0’s in C++ program
- Find largest subtree having identical left and right subtrees in Python
- Find the Pattern of 1’s inside 0’s using C++
- Segregate 0’s and 1’s in an array list using Python?
- Find the index of first 1 in a sorted array of 0's and 1's in C++
- Count the number of 1’s and 0’s in a binary array using STL in C++
- Count subarrays consisting of only 0’s and only 1’s in a binary array in C++
