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

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Trees problems