C++ Largest Subtree having Equal No of 1's and 0's

C++Server Side ProgrammingProgramming

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.

raja
Updated on 25-Nov-2021 09:49:47

Advertisements