Given a Binary Tree, the task is to find the maximum of all the XOR value of all the nodes in the path from the root to leaf.
Input: 2 / \ 1 4 / \ 10 8 Output: 11 Explanation: All the paths are: 2-1-10 XOR-VALUE = 9 2-1-8 XOR-VALUE = 11 2-4 XOR-VALUE = 6 Input: 2 / \ 1 4 / \ / \ 10 8 5 10 Output: 12
To solve the question mentioned above we have 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.
XOR of current node’s path = (XOR of the path till the parent) ^ (current node value)
If the node is a leaf node that is left and the right child for the current nodes are NULL then we compute the max-Xor, as
max-Xor = max(max-Xor, cur-Xor).
Below is the implementation of the above approach:
Time Complexity: We are iterating over each node only once, therefore it will take O(N) time where N is the number of nodes in the Binary tree.
Auxiliary Space Complexity: The Auxiliary Space complexity will be O(1), as there is no extra space used
- Maximum Path Sum in a Binary Tree
- Find the maximum path sum between two leaves of a binary tree
- Maximum Consecutive Increasing Path Length in Binary Tree
- Find the maximum sum leaf to root path in a Binary Tree
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Count of 1's in any path in a Binary Tree
- XOR of path between any two nodes in a Binary Tree
- Print all Coprime path of a Binary Tree
- Print path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree | Set 2
- Longest Path with Same Values in a Binary Tree
- Minimum sum path between two leaves of a binary tree
- Maximum XOR with given value in the path from root to given node in the tree
- Find all root to leaf path sum of a Binary Tree
- Print path from root to a given node in a binary tree
- Sort the path from root to a given node in a Binary Tree
- Print the longest path from root to leaf in a Binary tree
- Boundary Root to Leaf Path traversal of a Binary Tree
Improved By : SHIVAMSINGH67