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:
Node object into an array in an arbitrary order.findRoot, and your function should find and return the root Node object in the array.Node object 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:
[1, 5 * 10⁴].Follow up:
class Solution:
def findRoot(self, tree: List['Node']) -> 'Node':
# set that contains all the child nodes.
seen = set()
# add all the child nodes into the set
for node in tree:
for child in node.children:
# we could either add the value or the node itself.
seen.add(child.val)
# find the node that is not in the child node set.
for node in tree:
if node.val not in seen:
return nodeWhere is the length of the input list, which is also the number of nodes in the N-ary tree.
class Solution:
def findRoot(self, tree: List['Node']) -> 'Node':
value_sum = 0
for node in tree:
# the value is added as a parent node
value_sum += node.val
for child in node.children:
# the value is deducted as a child node.
value_sum -= child.val
# the value of the root node is `value_sum`
for node in tree:
if node.val == value_sum:
return nodeWhere is the length of the input list, which is also the number of nodes in the N-ary tree.