Saturday, November 23, 2024
Google search engine
HomeData Modelling & AIConvert a given Binary tree to a tree that holds Logical OR...

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

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

Example: 

Input: 
       1
     /   \
    1      0
  /  \    /  \
 0    1  1    1

Output: 0 1 1 1 1 1 1
Explanation: 
Given Tree
       1
     /   \
    1      0
  /  \    /  \
 0    1  1    1

After Processing
       1
     /   \
    1      1
  /  \    /  \
 0    1  1    1

Approach: 
The idea is to traverse given binary tree in postorder fashion because in postorder traversal both the children of the root has already been visited before the root itself. 
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 logic OR of its child data.

Below is the implementation of the above approach:  

C++




// C++ code to convert a
// given binary tree to
// a tree that holds
// logical OR property.
#include <bits/stdc++.h>
using namespace std;
 
// Structure of the 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
// OR 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()
{
 
    Node* root = newNode(1);
    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);
 
    convertTree(root);
    printInorder(root);
    return 0;
}


Python




# Python program to convert a
# given binary tree to
# a tree that holds
# logical OR property.
 
# Function that allocates a new
class newNode:
 
    # Construct to create a new node
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None
 
# Convert the given tree
# to a tree where each node
# is logical or of its children
# The main idea is to do
# Postorder traversal
def convertTree(root) :
 
    if (root == None) :
        return
 
    # First recur on left child
    convertTree(root.left)
 
    # Then recur on right child
    convertTree(root.right)
 
    if (root.left and root.right):
        root.data \
        = ((root.left.data) |
                    (root.right.data))
 
def printInorder(root) :
 
    if (root == None) :
        return
 
    # First recur on left child
    printInorder(root.left)
 
    # Then print the data of node
    print( root.data, end = " ")
 
    # Now recur on right child
    printInorder(root.right)
 
# Driver Code
if __name__ == '__main__':
     
    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)
 
    convertTree(root)
    printInorder(root)


Java




// Java code to convert a
// given binary tree to a
// tree that holds logical
// OR property.
class GfG {
 
    // Structure of the binary tree
    static class Node {
        int data;
        Node left;
        Node right;
    }
 
    // Function to create a new node
    static Node newNode(int key)
    {
        Node node = new Node();
        node.data = key;
        node.left = null;
        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
    static 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);
    }
 
    // Function to print inorder traversal
    // of the tree
    static void printInorder(Node root)
    {
        if (root == null)
            return;
 
        // First recur on left child
        printInorder(root.left);
 
        // Then print the data of node
        System.out.print(root.data + " ");
 
        // Now recur on right child
        printInorder(root.right);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        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);
 
        convertTree(root);
        printInorder(root);
    }
}


C#




// C# code to convert a given
// binary tree to a tree that
// holds logical AND property.
using System;
 
class GfG {
 
    // Structure of binary tree
    class Node {
        public int data;
        public Node left;
        public Node right;
    }
 
    // Function to create a new node
    static Node newNode(int key)
    {
        Node node = new Node();
        node.data = key;
        node.left = null;
        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
    static 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);
    }
 
    // Function to perform the inorder
    // traversal
    static void printInorder(Node root)
    {
        if (root == null)
            return;
 
        // First recur on left child
        printInorder(root.left);
 
        // then print the data of node
        Console.Write(root.data + " ");
 
        // now recur on right child
        printInorder(root.right);
    }
 
    // Driver code
    public static void Main()
    {
 
        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);
 
        convertTree(root);
        printInorder(root);
    }
}
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// Javascript code to convert a given
// binary tree to a tree that
// holds logical AND property.
// Structure of binary tree
class Node {
 
    constructor()
    {
        this.data = 0;
        this.left = null;
        this.right = null;
    }
}
 
// Function to create a new node
function newNode(key)
{
    var node = new Node();
    node.data = key;
    node.left = null;
    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
function convertTree(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);
}
 
// Function to perform the inorder
// traversal
function printInorder(root)
{
    if (root == null)
        return;
         
    // First recur on left child
    printInorder(root.left);
     
    // then print the data of node
    document.write(root.data + " ");
     
    // now recur on right child
    printInorder(root.right);
}
 
// Driver code
var 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);
convertTree(root);
printInorder(root);
 
// This code is contributed by rrrtnx.
</script>


Output: 

0 1 1 1 1 1 1

 

Time Complexity: O(N) where N is the number of nodes.
Auxiliary Space: O(H) where H is the height of the tree.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments