1506. Find Root of N-Ary Tree
Problem
Given all nodes of an N-ary tree, find and return the value of the root node.
An N-ary tree is described by two arrays of unique positive integers.
parentValues holds the value of every node in the tree, including the root, in arbitrary order. childValues holds the value of every child, flattened across all child lists, so each non-root node contributes exactly one entry. The tree is well formed: exactly one node is not anybody's child, and that node is the root.
Return the value of the root.
Examples
Input: [[1,2,3,4],[2,3,4]]
Output: 1
Input: [[10,20,30,40,50],[20,40,30,50]]
Output: 10
Hints
Use XOR or a hash set to find the value that is never a child.
The root is not a child of any other node.
Sum up all values and subtract the sum of the child values.
Related Problems
1506. Find Root of N-Ary Tree
Given all nodes of an N-ary tree, find and return the value of the root node.