Measure the Maximum Root-to-Leaf Depth of an N-ary Tree

Measure the Maximum Root-to-Leaf Depth of an N-ary Tree

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Given Node* root, return the greatest number of nodes on a root-to-leaf path. An empty tree has depth zero. Node exposes val and vector<Node*> children; retain this native node interface. In displayed level-order examples, null separates each parent's children group. ```cpp class Solution { public: int maxDepth(Node* root); }; ``` ### Examples **Example 1:** ```text root = [8,null,2,5,null,9] Output: 3 Explanation: Root 8 has children 2 and 5; node 2 has child 9. ``` **Example 2:** ```text root = [] Output: 0 Explanation: There is no root. ``` ### Constraints - The N-ary tree has 0..10000 nodes. Its depth does not exceed 1000. ### Clarifying Questions Count nodes along the path and preserve the Node.children interface and empty-tree case. ```hint Read the boundary condition How should a child's depth relate to its parent's depth? ``` ### Contract considerations Count nodes along the path and preserve the Node.children interface and empty-tree case.
Amazon logo
Amazon
Jul 7, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given Node* root, return the greatest number of nodes on a root-to-leaf path. An empty tree has depth zero. Node exposes val and vector<Node*> children; retain this native node interface. In displayed level-order examples, null separates each parent's children group.

class Solution {
public:
    int maxDepth(Node* root);
};

Examples

Example 1:

root = [8,null,2,5,null,9]
Output: 3
Explanation: Root 8 has children 2 and 5; node 2 has child 9.

Example 2:

root = []
Output: 0
Explanation: There is no root.

Constraints

  • The N-ary tree has 0..10000 nodes. Its depth does not exceed 1000.

Clarifying Questions Guidance

Count nodes along the path and preserve the Node.children interface and empty-tree case.

Contract considerations

Count nodes along the path and preserve the Node.children interface and empty-tree case.