Convert given binary tree to a XOR tree
Last Updated :
09 May, 2022
Given a Binary Tree where each node has a value of either 0 or 1, the task is to convert the given Binary tree to an XOR tree i.e a tree such that each node value is the logical XOR between its children.
Note: Leaf nodes and nodes with one child are not considered as they don’t have both children.
Examples:
Input:
1
/ \
1 0
/ \ / \
0 1 0 1
Output:
0
/ \
1 1
/ \ / \
0 1 0 1
Explanation: Each node value is the Logical XOR of its children.
For example, the right most node in 2nd level is the Logical XOR of its children. i.e., 0 ^ 1 = 1
Input:
1
/ \
0 0
/ \ /
1 0 1
Output:
1
/ \
1 0
/ \ /
1 0 1
Explanation: Each node has value same as the Logical XOR of its children.
For example, the left most node in 2nd level is the Logical Xor of its children. i.e., 1 ^ 0 = 1.
The rightmost child of second level has value unchanged because it has only one child.
Approach: The problem can be solved based on the following idea:
The idea is to perform a postorder traversal of the tree because in postorder traversal both the children are visited before the root node. During the traversal keep on performing the XOR of the children and change the value of the current node as per that.
Follow the steps to solve the problem:
- For each node recursively check for the children of the nodes.
- If the node has only one child or no child then do nothing.
- Else if the node has both children, then simply update the node’s data with the logical XOR of its children.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int key)
{
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);
cout << 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(0);
root->right->right = newNode(1);
cout << "Before conversion: " ;
printInorder(root);
convertTree(root);
cout << "\nAfter conversion: " ;
printInorder(root);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static class Node {
int data;
Node left, right;
}
public static Node newNode( int key)
{
Node node = new Node();
node.data = key;
node.left = node.right = null ;
return node;
}
public 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;
}
public 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( 1 );
root.left = newNode( 1 );
root.right = newNode( 0 );
root.left.left = newNode( 0 );
root.left.right = newNode( 1 );
root.right.left = newNode( 0 );
root.right.right = newNode( 1 );
System.out.print( "Before conversion: " );
printInorder(root);
convertTree(root);
System.out.print( "\nAfter conversion: " );
printInorder(root);
}
}
|
Python3
class Node:
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 ! = None and root.right ! = None ):
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)
root = Node( 1 )
root.left = Node( 1 )
root.right = Node( 0 )
root.left.left = Node( 0 )
root.left.right = Node( 1 )
root.right.left = Node( 0 )
root.right.right = Node( 1 )
print ( "Before conversion:" ,end = " " )
printInorder(root)
convertTree(root)
print ()
print ( "After conversion:" ,end = " " )
printInorder(root)
|
C#
using System;
public class GFG
{
public class Node {
public int data;
public Node left, right;
}
public static Node newNode( int key)
{
Node node = new Node();
node.data = key;
node.left = node.right = null ;
return node;
}
public 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;
}
public static void printInorder(Node root)
{
if (root == null )
return ;
printInorder(root.left);
Console.Write(root.data + " " );
printInorder(root.right);
}
public static void Main( string [] args)
{
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(0);
root.right.right = newNode(1);
Console.Write( "Before conversion: " );
printInorder(root);
convertTree(root);
Console.Write( "\nAfter conversion: " );
printInorder(root);
}
}
|
Javascript
<script>
class Node {
constructor(key) {
this .data = key;
this .left = null ;
this .right = null ;
}
};
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);
}
let root = new Node(1);
root.left = new Node(1);
root.right = new Node(0);
root.left.left = new Node(0);
root.left.right = new Node(1);
root.right.left = new Node(0);
root.right.right = new Node(1);
document.write( "Before conversion: " );
printInorder(root);
convertTree(root);
document.write('<br>' + "After conversion: " );
printInorder(root);
</script>
|
Output
Before conversion: 0 1 1 1 0 0 1
After conversion: 0 1 1 0 0 1 1
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...