Convert a given Binary tree to a tree that holds Logical AND property

2

Given a Binary Tree (Every node has at most 2 children) where each node has value either 0 or 1. Convert a given Binary tree to a tree that holds Logical AND property, i.e., each node value should be the logical AND between its children.

Examples:

Input : The below tree doesn’t hold the logical AND property
        convert it to a tree that holds the property.
             1
           /   \
          1     0
         / \   / \
        0   1 1   1 
Output :
             0
           /   \
          0     1
         / \   / \
        0   1 1   1 

The idea is to traverse given binary tree in postorder fashion. For each node check (recursively) if the node has one children then we don’t have any need to check else if the node has both its child then simply update the node data with the logical AND of its child data.

// C++ code to covert a given binary tree
// to a tree that holds logical AND property.
#include<bits/stdc++.h>
using namespace std;

// Structure of binary tree
struct Node
{
    int data;
    struct Node* left;
    struct Node* right;
};

// function to create a new node
struct Node* newNode(int key)
{
    struct Node* node = new Node;
    node->data= key;
    node->left = node->right = NULL;
    return node;
}

// Convert the given tree to a tree where
// each node is logical AND of its children
// The main idea is to do Postorder traversal
void convertTree(Node *root)
{
    if (root == NULL)
        return;

    /* first recur on left child */
    convertTree(root->left);

    /* then recur on right child */
    convertTree(root->right);

    if (root->left != NULL && root->right != NULL)
        root->data = (root->left->data) &
                     (root->right->data);
}

void printInorder(Node* root)
{
    if (root == NULL)
        return;

    /* first recur on left child */
    printInorder(root->left);

    /* then print the data of node */
    printf("%d ", root->data);

    /* now recur on right child */
    printInorder(root->right);
}

// main function
int main()
{
    /* Create following Binary Tree
             1
           /   \
          1     0
         / \   / \
        0   1 1   1
             */

    Node *root=newNode(0);
    root->left=newNode(1);
    root->right=newNode(0);
    root->left->left=newNode(0);
    root->left->right=newNode(1);
    root->right->left=newNode(1);
    root->right->right=newNode(1);
    printf("\n Inorder traversal before conversion ");
    printInorder(root);

    convertTree(root);

    printf("\n Inorder traversal after conversion ");
    printInorder(root);
    return 0;
}

Output:

 Inorder traversal before conversion 0 1 1 0 1 0 1 
 Inorder traversal after conversion 0 0 1 0 1 1 1 

This article is contributed by Roshni Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 11 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.