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.

AnswerBot
1y

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

Help your peers!
Add answer anonymously...
Protium Finance Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter