XOR Query on Tree Problem
Given a tree with a root at node 0 and N vertices connected with N-1 edges, and an array QUERY
of size Q, where each element in the array represents a node in the tree. For each node in the QUERY
array, determine the XOR of all values of nodes within its subtree.
Example:
Input:
T = 1
N = 3, Q = 2
Edges:
0 1
1 2
Query: 0 1
Output:
2 XOR value for subtrees of nodes 0 and 1 respectively
Explanation:
If the array of node values is assumed to be [value0, value1, value2], the subtree rooted at 0 would include all nodes, hence the XOR would be value0 ^ value1 ^ value2. For the subtree rooted at 1, it would include nodes 1 and 2, hence the XOR would be value1 ^ value2.
Constraints:
- 1 <= T <= 10
- 0 <= N, Q <= 105
- 0 <= QUERY[i] < N
The XOR operation is performed using the bitwise operator (^).
Note:
No need to print anything as it is handled elsewhere, just implement the respective function to obtain the desired results.
This question is about finding the XOR of all values of nodes in the sub-tree of a given node in a tree.
Read the input values for the number of test cases, number of nodes, and number of queries.
Const...read more
Top Protium Finance Software Developer interview questions & answers
Popular interview questions of Software Developer
Reviews
Interviews
Salaries
Users/Month