1506. Find Root of N-Ary Tree
Problem
Given all nodes of an N-ary tree in a list, find and return the root node.
You are given all the nodes of an N-ary tree in an array nodes. Each node has a val and a list of children. Find and return the root node of the tree.
The root is the only node that is not a child of any other node.
Examples
Input: [[1,[3,2,4]],[3,[5]],[2,[]],[4,[]],[5,[]]]
Output: 1
Input: [[1,[3]],[3,[2,4]],[2,[]],[4,[]]]
Output: 1
Hints
Use XOR or hash set to find the node that is never a child.
The root is not a child of any other node.
Sum up all values and subtract sum of children's values.
Related Problems
1506. Find Root of N-Ary Tree
Given all nodes of an N-ary tree in a list, find and return the root node.