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 * 104].
Follow up:
Problem Overview: You receive a list of nodes representing an N-ary tree. Each node contains its value and references to its children, but the root node is not explicitly given. Your task is to determine which node is the root.
The key observation: every node appears exactly once as a child except the root. The root is the only node that never appears in any child list.
Approach 1: Hash Set of Children (O(n) time, O(n) space)
Traverse every node and add each child node to a hash set. After collecting all children, iterate through the original list of nodes. The node that never appears in the set must be the root. The logic works because every non-root node appears exactly once as a child somewhere in the tree.
This approach uses a simple membership lookup with hash tables. Insert and lookup operations are O(1) on average, making the overall solution linear. The tradeoff is extra memory proportional to the number of nodes.
Approach 2: Value Sum / Bit Manipulation Trick (O(n) time, O(1) space)
Instead of storing children, accumulate the sum of all node values and subtract the values of all child nodes. Since every node except the root appears exactly once as a child, all non-root values cancel out. The remaining value corresponds to the root node.
A similar trick uses XOR instead of addition, which avoids overflow and fits naturally with bit manipulation. XOR all node values, then XOR all child values. Identical values cancel out, leaving the root's value. Finally, scan the node list to return the node with that value. This approach runs in linear time with constant extra memory.
Approach 3: Parent Tracking with DFS Insight (O(n) time, O(n) space)
Another way is to explicitly record parent relationships. Traverse every node and mark each child as having a parent. After processing the entire list, the node without a recorded parent is the root. This method mirrors how you'd discover roots during a Depth-First Search traversal when building a tree from edges.
The idea is conceptually simple but uses extra space similar to the hash set approach. It is useful when working with general graph-style inputs where parent relationships must be tracked.
Recommended for interviews: The hash set approach is the most straightforward and communicates the key observation clearly. The XOR/sum trick shows deeper insight and achieves O(1) extra space, which often impresses interviewers. Start with the set-based explanation to demonstrate understanding of the tree structure, then mention the bit manipulation optimization.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Hash Set of Children | O(n) | O(n) | Most intuitive solution; good when clarity is preferred over memory optimization |
| Value Sum / XOR Trick | O(n) | O(1) | Best when minimizing extra space; common interview optimization |
| Parent Tracking | O(n) | O(n) | Useful when explicitly building parent relationships or validating tree structure |
1506. Find Root of N-Ary Tree • Muerde un zombie • 561 views views
Watch 7 more video solutions →Practice Find Root of N-Ary Tree with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor