Convert a given Binary tree to a tree that holds Logical OR property
Last Updated :
18 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode( int key)
{
struct Node* node = new Node;
node->data = key;
node->left = node->right = NULL;
return node;
}
void convertTree(Node* root)
{
if (root == NULL)
return ;
convertTree(root->left);
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 ;
printInorder(root->left);
printf ( "%d " , root->data);
printInorder(root->right);
}
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
class newNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def convertTree(root) :
if (root = = None ) :
return
convertTree(root.left)
convertTree(root.right)
if (root.left and root.right):
root.data \
= ((root.left.data) |
(root.right.data))
def printInorder(root) :
if (root = = None ) :
return
printInorder(root.left)
print ( root.data, end = " " )
printInorder(root.right)
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
class GfG {
static class Node {
int data;
Node left;
Node right;
}
static Node newNode( int key)
{
Node node = new Node();
node.data = key;
node.left = null ;
node.right = null ;
return node;
}
static void convertTree(Node root)
{
if (root == null )
return ;
convertTree(root.left);
convertTree(root.right);
if (root.left != null
&& root.right != null )
root.data
= (root.left.data)
| (root.right.data);
}
static void printInorder(Node root)
{
if (root == null )
return ;
printInorder(root.left);
System.out.print(root.data + " " );
printInorder(root.right);
}
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#
using System;
class GfG {
class Node {
public int data;
public Node left;
public Node right;
}
static Node newNode( int key)
{
Node node = new Node();
node.data = key;
node.left = null ;
node.right = null ;
return node;
}
static void convertTree(Node root)
{
if (root == null )
return ;
convertTree(root.left);
convertTree(root.right);
if (root.left != null
&& root.right != null )
root.data
= (root.left.data)
| (root.right.data);
}
static void printInorder(Node root)
{
if (root == null )
return ;
printInorder(root.left);
Console.Write(root.data + " " );
printInorder(root.right);
}
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);
}
}
|
Javascript
<script>
class Node {
constructor()
{
this .data = 0;
this .left = null ;
this .right = null ;
}
}
function newNode(key)
{
var node = new Node();
node.data = key;
node.left = null ;
node.right = null ;
return node;
}
function convertTree(root)
{
if (root == null )
return ;
convertTree(root.left);
convertTree(root.right);
if (root.left != null
&& root.right != null )
root.data
= (root.left.data)
| (root.right.data);
}
function printInorder(root)
{
if (root == null )
return ;
printInorder(root.left);
document.write(root.data + " " );
printInorder(root.right);
}
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);
</script>
|
Time Complexity: O(N) where N is the number of nodes.
Auxiliary Space: O(H) where H is the height of the tree.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...