Find Root of N-Ary Tree Problem
Description
LeetCode Problem 1506.
You are given all the nodes of an N-ary tree as an array of Node objects, where each node has a unique value.
Return the root of the N-ary tree.
Custom testing:
An N-ary tree can be serialized as represented in its level order traversal where each group of children is separated by the null value (see examples).

For example, the above tree is serialized as [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14].
The testing will be done in the following way:
- The input data should be provided as a serialization of the tree.
- The driver code will construct the tree from the serialized input data and put each
Nodeobject into an array in an arbitrary order. - The driver code will pass the array to
findRoot, and your function should find and return the rootNodeobject in the array. - The driver code will take the returned
Nodeobject and serialize it. If the serialized value and the input data are the same, the test passes.
Example 1:

Input: tree = [1,null,3,2,4,null,5,6]
Output: [1,null,3,2,4,null,5,6]
Explanation: The tree from the input data is shown above.
The driver code creates the tree and gives findRoot the Node objects in an arbitrary order.
For example, the passed array could be [Node(5),Node(4),Node(3),Node(6),Node(2),Node(1)] or [Node(2),Node(6),Node(1),Node(3),Node(5),Node(4)].
The findRoot function should return the root Node(1), and the driver code will serialize it and compare with the input data.
The input data and serialized Node(1) are the same, so the test passes.
Example 2:

Input: tree = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Constraints:
- The total number of nodes is between
[1, 5 * 10^4]. - Each node has a unique value.
Follow up:
- Could you solve this problem in constant space complexity with a linear time algorithm?
Solution
Approach: XOR of All Values
Every node except the root is the child of exactly one other node. So if we go through the array and list each node's own value and the values of all its children, every non-root value shows up exactly twice (once as a node, once as a child) and the root's value shows up once.
XOR cancels pairs: a ^ a = 0. XOR together every node's value and every child's value, and all that is left is the root's value. A second pass finds the node with that value and returns it. This works because the values are unique.
The same idea works with a sum: add every node's value and subtract every child's value. XOR is a little neater because it can never overflow.
Complexity: O(n) time, since every node is visited once as a node and once as a child, and O(1) extra space, which answers the follow-up.
"""
# Definition for a Node.
class Node:
def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None):
self.val = val
self.children = children if children is not None else []
"""
class Solution:
def findRoot(self, tree: List['Node']) -> 'Node':
# Every value except the root's shows up twice: once as a node, once as a child.
x = 0
for node in tree:
x ^= node.val
for child in node.children:
x ^= child.val
for node in tree:
if node.val == x:
return node
return None