Problem 445656 · easy · Phase 04 Non-Linear Data Structures

Depth of an Org Chart

n-ary tree · BFS · DFS

A company org chart is an N-ary tree of Node objects: each node has a value val and a list children (possibly empty). Return the depth of the chart, defined as the number of nodes on the longest path from the root down to a leaf. An empty chart (None) has depth 0.

Examples

Input:  root = Node("ceo", [Node("cto", [Node("dev")]), Node("cfo")])
Output: 3
Explanation: ceo -> cto -> dev has three nodes.

Input:  root = Node("solo")
Output: 1

Constraints

  • 0 <= number of nodes <= 10**4; a node may have up to 5000 children
  • The chart may be a single chain up to 1500 nodes deep, so prefer an iterative traversal
  • Target: O(n) time

Goals

  • Traverse a tree whose nodes hold a list of children
  • Track the depth of every node while traversing iteratively
Starting Python…