Skip to main content

Find Root of N-Ary Tree

In this page, we will solve the Find Root of N-Ary Tree problem using different approaches: counting the degree of each node and a hash set approach. We will provide the implementation of the solution in JavaScript, TypeScript, Python, Java, C++, and more.

Problem Description​

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.

Each Node object has the following properties:

  • int val: the value of the node.
  • List<Node> children: the list of children of the node.

Examples​

Example 1:

Input: 
Input: [1, null, 3, 2, 4, null, 5, 6]
Output: 1
Explanation: The root of the tree is 1.

Example 2:

Input: 
Input: [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
Explanation: The root of the tree is 1.

Constraints​

  • The total number of nodes is between [1, 5 * 10^4].

Solution for Find Root of N-Ary Tree​

Intuition and Approach​

To find the root of an N-ary tree, we can use the following approaches:

  1. Counting the Degree: In this approach, we count the in-degrees (number of times a node appears as a child) of all nodes. The node with an in-degree of zero is the root.
  2. Hash Set: We use a hash set to keep track of all nodes and another hash set for all children nodes. The difference between these sets will give us the root node.

Approach 1: Counting the Degree​

Implementation​

Live Editor
function findRootOfNAryTree() {
  class Node {
    constructor(val) {
      this.val = val;
      this.children = [];
    }
  }

  const nodes = [
    new Node(1),
    new Node(2),
    new Node(3),
    new Node(4),
    new Node(5),
    new Node(6)
  ];

  nodes[0].children = [nodes[1], nodes[2], nodes[3]];
  nodes[1].children = [nodes[4], nodes[5]];

  const findRoot = function(treeNodes) {
    const inDegree = new Map();
    for (const node of treeNodes) {
      if (!inDegree.has(node)) {
        inDegree.set(node, 0);
      }
      for (const child of node.children) {
        inDegree.set(child, (inDegree.get(child) || 0) + 1);
      }
    }
    for (const [node, degree] of inDegree) {
      if (degree === 0) return node;
    }
    return null;
  };

  const root = findRoot(nodes);
  return (
    <div>
      <p>
        <b>Input:</b> {JSON.stringify(nodes.map(node => node.val))}
      </p>
      <p>
        <b>Output:</b> {root ? root.val : null}
      </p>
    </div>
  );
}
Result
Loading...

Code in Different Languages​

Written by @manishh12
 function findRoot(treeNodes) {
const inDegree = new Map();
for (const node of treeNodes) {
if (!inDegree.has(node)) {
inDegree.set(node, 0);
}
for (const child of node.children) {
inDegree.set(child, (inDegree.get(child) || 0) + 1);
}
}
for (const [node, degree] of inDegree) {
if (degree === 0) return node;
}
return null;
}

Complexity Analysis​

  • Time Complexity: O(N)O(N), where N is the number of nodes in the tree.
  • Space Complexity: O(N)O(N), for storing the in-degree map.
Note

By using both counting the degree and hash set approaches, we can efficiently find the root of an N-ary tree. The choice between the two approaches depends on the specific requirements and constraints of the problem.

References​