Print all the root-to-leaf paths of a Binary Tree whose XOR is non-zero
Last Updated :
23 Jan, 2023
Given a Binary Tree, the task is to print all root-to-leaf 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 the path is non-zero or not, if it is then we print the entire path.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
struct Node *left, *right;
};
Node* newNode( int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
bool PathXor(vector< int >& path)
{
int ans = 0;
for ( auto x : path) {
ans ^= x;
}
return (ans != 0);
}
void printPaths(vector< int >& path)
{
for ( auto x : path) {
cout << x << " " ;
}
cout << endl;
}
void findPaths( struct Node* root,
vector< int >& path)
{
if (root == NULL)
return ;
path.push_back(root->key);
findPaths(root->left, path);
findPaths(root->right, path);
if (root->left == NULL
&& root->right == NULL) {
if (PathXor(path)) {
printPaths(path);
}
}
path.pop_back();
}
void printPaths( struct Node* node)
{
vector< int > path;
findPaths(node, path);
}
int main()
{
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);
printPaths(root);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node
{
int key;
Node left, right;
};
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null ;
return (temp);
}
static boolean PathXor(Vector<Integer> path)
{
int ans = 0 ;
for ( int x : path)
{
ans ^= x;
}
return (ans != 0 );
}
static void printPaths(Vector<Integer> path)
{
for ( int x : path)
{
System.out.print(x + " " );
}
System.out.println();
}
static void findPaths(Node root,
Vector<Integer> path)
{
if (root == null )
return ;
path.add(root.key);
findPaths(root.left, path);
findPaths(root.right, path);
if (root.left == null &&
root.right == null )
{
if (PathXor(path))
{
printPaths(path);
}
}
path.remove(path.size() - 1 );
}
static void printPaths(Node node)
{
Vector<Integer> path = new Vector<Integer>();
findPaths(node, path);
}
public static void main(String[] args)
{
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 );
printPaths(root);
}
}
|
Python3
class Node:
def __init__( self , key):
self .key = key
self .left = None
self .right = None
def PathXor(path: list ) - > bool :
ans = 0
for x in path:
ans ^ = x
return (ans ! = 0 )
def printPathsList(path: list ) - > None :
for x in path:
print (x, end = " " )
print ()
def findPaths(root: Node, path: list ) - > None :
if (root = = None ):
return
path.append(root.key)
findPaths(root.left, path)
findPaths(root.right, path)
if (root.left = = None and
root.right = = None ):
if (PathXor(path)):
printPathsList(path)
path.pop()
def printPaths(node: Node) - > None :
path = []
newNode = node
findPaths(newNode, path)
if __name__ = = "__main__" :
root = Node( 10 )
root.left = Node( 10 )
root.right = Node( 3 )
root.right.left = Node( 10 )
root.right.right = Node( 3 )
root.right.left.left = Node( 7 )
root.right.left.right = Node( 3 )
root.right.right.left = Node( 42 )
root.right.right.right = Node( 13 )
root.right.right.right.left = Node( 7 )
printPaths(root)
|
C#
using System;
using System.Collections.Generic;
class GFG{
class Node
{
public int key;
public Node left, right;
};
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null ;
return (temp);
}
static bool PathXor(List< int > path)
{
int ans = 0;
foreach ( int x in path)
{
ans ^= x;
}
return (ans != 0);
}
static void printPaths(List< int > path)
{
foreach ( int x in path)
{
Console.Write(x + " " );
}
Console.WriteLine();
}
static void findPaths(Node root,
List< int > path)
{
if (root == null )
return ;
path.Add(root.key);
findPaths(root.left, path);
findPaths(root.right, path);
if (root.left == null &&
root.right == null )
{
if (PathXor(path))
{
printPaths(path);
}
}
path.RemoveAt(path.Count - 1);
}
static void printPaths(Node node)
{
List< int > path = new List< int >();
findPaths(node, path);
}
public static void Main(String[] args)
{
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);
printPaths(root);
}
}
|
Javascript
<script>
class Node
{
constructor(key) {
this .left = null ;
this .right = null ;
this .key = key;
}
}
function newNode(key)
{
let temp = new Node(key);
return (temp);
}
function PathXor(path)
{
let ans = 0;
for (let x = 0; x < path.length; x++)
{
ans ^= path[x];
}
if (ans != 0)
{
return true ;
}
else {
return false ;
}
}
function printPaths(path)
{
for (let x = 0; x < path.length; x++)
{
document.write(path[x] + " " );
}
document.write( "</br>" );
}
function findPaths(root, path)
{
if (root == null )
return ;
path.push(root.key);
findPaths(root.left, path);
findPaths(root.right, path);
if (root.left == null &&
root.right == null )
{
if (PathXor(path))
{
printPaths(path);
}
}
path.pop();
}
function printpaths(node)
{
let path = [];
findPaths(node, path);
}
let 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);
printpaths(root);
</script>
|
Output:
10 3 10 7
10 3 3 42
Time complexity: O(N) where N is no of nodes in given binary tree
Space Complexity: O(h) where h is the height of the binary tree.
Share your thoughts in the comments
Please Login to comment...