Given a Binary Tree, the task is to print all the paths of this tree whose xor value is non-zero.
Examples:
Input:
10
/ \
10 3
/ \
10 3
/ \ / \
7 3 42 13
/
7
Output:
10 3 10 7
10 3 3 42
Explanation:
All the paths in the given binary tree are :
{10, 10} xor value of the path is
= 10 ^ 10 = 0
{10, 3, 10, 7} xor value of the path is
= 10 ^ 3 ^ 10 ^ 7 != 0
{10, 3, 3, 42} xor value of the path is
= 10 ^ 3 ^ 3 ^ 42 != 0
{10, 3, 10, 3} xor value of the path is
= 10 ^ 3 ^ 10 ^ 3 = 0
{10, 3, 3, 13, 7} xor value of the path is
= 10 ^ 3 ^ 3 ^ 13 ^ 7 = 0.
Hence, {10, 3, 10, 7} and {10, 3, 3, 42} are
the paths whose xor value is non-zero.
Input:
5
/ \
21 77
/ \ \
5 21 16
\ /
5 3
Output :
5 21 5
5 77 16 3
Explanation:
{5, 21, 5} and {5, 77, 16, 3} are the paths
whose xor value is non-zero.
Approach:
To solve the problem mentioned above the main idea is to traverse the tree and check if the xor of all the elements in that path is zero or not.
- We need to traverse the tree recursively using Pre-Order Traversal.
- For each node keep calculating the XOR of the path from root till the current node.
- If the node is a leaf node that is left and the right child for the current nodes are NULL then we check if the xor value of path is non-zero or not, if it is then we print the entire path.
Below is the implementation of the above approach:
// C++ program to Print all the Paths of a // Binary Tree whose XOR gives a non-zero value #include <bits/stdc++.h> using namespace std; // A Tree node struct Node { int key; struct Node *left, *right; }; // Function to create a new node Node* newNode(int key) { Node* temp = new Node; temp->key = key; temp->left = temp->right = NULL; return (temp); } // Function to check whether Path // is has non-zero xor value or not bool PathXor(vector<int>& path) { int ans = 0; // Iterating through the array // to find the xor value for (auto x : path) { ans ^= x; } return (ans != 0); } // Function to print a path void printPaths(vector<int>& path) { for (auto x : path) { cout << x << " "; } cout << endl; } // Function to find the paths of // binary tree having non-zero xor value void findPaths(struct Node* root, vector<int>& path) { // Base case if (root == NULL) return; // Store the value in path vector path.push_back(root->key); // Recursively call for left sub tree findPaths(root->left, path); // Recursively call for right sub tree findPaths(root->right, path); // Condition to check, if leaf node if (root->left == NULL && root->right == NULL) { // Condition to check, // if path has non-zero xor or not if (PathXor(path)) { // Print the path printPaths(path); } } // Remove the last element // from the path vector path.pop_back(); } // Function to find the paths // having non-zero xor value void printPaths(struct Node* node) { vector<int> path; findPaths(node, path); } // Driver Code int main() { /* 10 / \ 10 3 / \ 10 3 / \ / \ 7 3 42 13 / 7 */ // Create Binary Tree as shown Node* root = newNode(10); root->left = newNode(10); root->right = newNode(3); root->right->left = newNode(10); root->right->right = newNode(3); root->right->left->left = newNode(7); root->right->left->right = newNode(3); root->right->right->left = newNode(42); root->right->right->right = newNode(13); root->right->right->right->left = newNode(7); // Print non-zero XOR Paths printPaths(root); return 0; } |
10 3 10 7 10 3 3 42
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print all k-sum paths in a binary tree
- Print all the paths from root, with a specified sum in Binary tree
- Print Palindromic Paths of Binary tree
- Given a binary tree, print all root-to-leaf paths
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Count all k-sum paths in a Binary Tree
- Count even paths in Binary Tree
- Count of Fibonacci paths in a Binary tree
- Count of exponential paths in a Binary Tree
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Root to leaf paths having equal lengths in a Binary Tree
- Count of root to leaf paths in a Binary Tree that form an AP
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Print all K-sum levels in a Binary Tree
- Print Binary Tree in 2-Dimensions
- Print Right View of a Binary Tree
- Print all full nodes in a Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

