Given a binary tree with distinct nodes and a pair of two nodes. The task is to find the XOR of all of the nodes which comes on the path between the given two nodes.
For Example, in the above binary tree for nodes (3, 5) XOR of path will be (3 XOR 1 XOR 0 XOR 2 XOR 5) = 5.
The idea is to make use of these two properties of XOR:
- XOR of same elements is zero.
- XOR of an element with zero gives the element itself.
Now, for each node find and store the XOR along the path from root to that node. This can be done using simple DFS. Now the XOR along path between any two nodes will be:
(XOR of path from root to first node) XOR (XOR of path from root to second node)
Explanation: There arises two different cases:
- If the two nodes are in different subtrees of root nodes. That is one in the left subtree and the other in the right subtree. In this case it is clear that the formulae written above will give the correct result as the path between the nodes goes through root with all distinct nodes.
- If the nodes are in the same subtree. That is either in the left subtree or in the right subtree. In this case you need to observe that path from root to the two nodes will have an intersection point before which the path is common for the two nodes from root. The XOR of this common path is calculated twice and cancels out, so it does not effect the result.
Note: For a single pair of nodes, it is not needed to store the path from roots to all nodes. This is efficient and written considering if there is a list of pair of nodes and for every pair we have to find XOR of path between the two nodes in a Binary Tree.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N), where N is the number of nodes.
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.
- Print path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree | Set 2
- Shortest path between two nodes in array like representation of binary tree
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Queries to check if the path between two nodes in a tree is a palindrome
- Sum of all odd nodes in the path connecting two given nodes
- Maximum XOR path of a Binary Tree
- Count of 1's in any path in a Binary Tree
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Print path from root to all nodes in a Complete Binary Tree
- Count nodes having smallest value in the path from root to itself in a Binary Tree
- Count nodes having highest value in the path from root to itself in a Binary Tree
- Count nodes having Bitwise XOR of all edges in their path from the root equal to K
- Find the maximum path sum between two leaves of a binary tree
- Minimum sum path between two leaves of a binary tree
- Check if two nodes are on same path in a tree | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.