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 = 1Input:
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++ program to convert a BT to XOR tree #include <bits/stdc++.h> using namespace std;
// Struct node of binary tree struct Node {
int data;
Node *left, *right;
}; // Function to create a new node Node* newNode( int key)
{ Node* node = new Node;
node->data = key;
node->left = node->right = NULL;
return node;
} // Utility function that converts // BT to XOR tree which holds // logical XOR property. void convertTree(Node* root)
{ // Base case
if (root == NULL)
return ;
// Recursion for left subtree
convertTree(root->left);
// Recursion for right subtree
convertTree(root->right);
// If the node has both childrens
// then update node's data as
// Logical Xor between childrens.
if (root->left != NULL
&& root->right != NULL)
root->data = root->left->data
^ root->right->data;
} // Function to print void printInorder(Node* root)
{ if (root == NULL)
return ;
// Recursion for left subtree
printInorder(root->left);
// Print the data
cout << root->data << " " ;
// Now recursion for right subtree
printInorder(root->right);
} // Driver code int main()
{ // Create a binary tree
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);
// Function to convert the tree
convertTree(root);
cout << "\nAfter conversion: " ;
printInorder(root);
return 0;
} |
// JAVA program to convert a BT to XOR tree import java.util.*;
class GFG
{ // Struct node of binary tree
public static class Node {
int data;
Node left, right;
}
// Function to create a new node
public static Node newNode( int key)
{
Node node = new Node();
node.data = key;
node.left = node.right = null ;
return node;
}
// Utility function that converts
// BT to XOR tree which holds
// logical XOR property.
public static void convertTree(Node root)
{
// Base case
if (root == null )
return ;
// Recursion for left subtree
convertTree(root.left);
// Recursion for right subtree
convertTree(root.right);
// If the node has both childrens
// then update node's data as
// Logical Xor between childrens.
if (root.left != null && root.right != null )
root.data = root.left.data ^ root.right.data;
}
// Function to print
public static void printInorder(Node root)
{
if (root == null )
return ;
// Recursion for left subtree
printInorder(root.left);
// Print the data
System.out.print(root.data + " " );
// Now recursion for right subtree
printInorder(root.right);
}
// Driver code
public static void main(String[] args)
{
// Create a binary tree
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);
// Function to convert the tree
convertTree(root);
System.out.print( "\nAfter conversion: " );
printInorder(root);
}
} // This code is contributed by Taranpreet |
# Python program for the above approach # Struct node of binary tree class Node:
def __init__( self ,key):
self .data = key
self .left = None
self .right = None
# Utility function that converts # BT to XOR tree which holds # logical XOR property. def convertTree(root):
# Base case
if (root = = None ):
return
# Recursion for left subtree
convertTree(root.left)
# Recursion for right subtree
convertTree(root.right)
# If the node has both childrens
# then update node's data as
# Logical Xor between childrens.
if (root.left ! = None and root.right ! = None ):
root.data = root.left.data ^ root.right.data
# Function to print def printInorder(root):
if (root = = None ):
return
# Recursion for left subtree
printInorder(root.left)
# Print the data
print (root.data ,end = " " )
# Now recursion for right subtree
printInorder(root.right)
# Driver code # Create a binary tree 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) # Function to convert the tree convertTree(root) print ()
print ( "After conversion:" ,end = " " )
printInorder(root) # This code is contributed by shinjanpatra |
using System;
// C# program to convert a BT to XOR tree public class GFG
{ // Struct node of binary tree
public class Node {
public int data;
public Node left, right;
}
// Function to create a new node
public static Node newNode( int key)
{
Node node = new Node();
node.data = key;
node.left = node.right = null ;
return node;
}
// Utility function that converts
// BT to XOR tree which holds
// logical XOR property.
public static void convertTree(Node root)
{
// Base case
if (root == null )
return ;
// Recursion for left subtree
convertTree(root.left);
// Recursion for right subtree
convertTree(root.right);
// If the node has both childrens
// then update node's data as
// Logical Xor between childrens.
if (root.left != null && root.right != null )
root.data = root.left.data ^ root.right.data;
}
// Function to print
public static void printInorder(Node root)
{
if (root == null )
return ;
// Recursion for left subtree
printInorder(root.left);
// Print the data
Console.Write(root.data + " " );
// Now recursion for right subtree
printInorder(root.right);
}
// Driver code
public static void Main( string [] args)
{
// Create a binary tree
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);
// Function to convert the tree
convertTree(root);
Console.Write( "\nAfter conversion: " );
printInorder(root);
}
} // This code is contributed by jana_sayantan. |
<script> // JavaScript code for the above approach
// Struct node of binary tree
class Node {
constructor(key) {
this .data = key;
this .left = null ;
this .right = null ;
}
};
// Utility function that converts
// BT to XOR tree which holds
// logical XOR property.
function convertTree(root) {
// Base case
if (root == null )
return ;
// Recursion for left subtree
convertTree(root.left);
// Recursion for right subtree
convertTree(root.right);
// If the node has both childrens
// then update node's data as
// Logical Xor between childrens.
if (root.left != null
&& root.right != null )
root.data = root.left.data
^ root.right.data;
}
// Function to print
function printInorder(root) {
if (root == null )
return ;
// Recursion for left subtree
printInorder(root.left);
// Print the data
document.write(root.data + " " );
// Now recursion for right subtree
printInorder(root.right);
}
// Driver code
// Create a binary tree
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);
// Function to convert the tree
convertTree(root);
document.write('<br>' + "After conversion: " );
printInorder(root);
// This code is contributed by Potta Lokesh
</script>
|
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)