Easy · Trees

N-ary tree height

Given the root of an N-ary tree, return its height: the number of nodes on the longest path from the root down to a leaf (0 for an empty tree).

Examples

Example 1

{
  "nodes": [
    {"val": 1, "children": [1, 2, 5]},
    {"val": 2, "children": []},
    {"val": 3, "children": [3, 4]},
    {"val": 4, "children": []},
    {"val": 5, "children": []},
    {"val": 6, "children": [6]},
    {"val": 7, "children": []}
  ]
}

Output: height 3

Example 2

{
  "nodes": [
    {"val": 9, "children": []}
  ]
}

Output: height 1

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Trees problems