Convert given binary tree to a XOR tree
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++
// C++ program to convert a BT to XOR tree #include <bits/stdc++.h>using namespace std; // Struct node of binary treestruct Node { int data; Node *left, *right;}; // Function to create a new nodeNode* 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 printvoid 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 codeint 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;} |
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)


