Grokking Tree Coding Patterns for Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Maximum Depth of N-ary Tree (easy)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a root of the n-ary tree, return the maximum depth of the tree.

The maximum depth refers to the longest path from the root node to any leaf node.

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value.

Examples

Example 1:

  • Input: root = [1,null,2,3,4,null,5]
Image
  • Expected Output: 3
  • Justification: The path from the root node 1 to the farthest leaf node 5 goes through nodes 1 -> 2 -> 5, making the depth 3.

Example 2:

  • Input: root = [1,null,2,3,4,null,5,null,6,7,8,null,9,10,null,11,12]
Image
  • Expected Output: 4
  • Justification: The longest path from the root node 1 to the farthest leaf node 11 or 12 is 1 -> 2 -> 5 -> 12, resulting in a depth of 4.

Example 3:

  • Input: root = [10,null,20,30,null,40,null,50,null,60]
Image
  • Expected Output: 4
  • Justification: The path from the root node 10 to the farthest leaf node 60 is 10 -> 20 -> 40 -> 60, making the depth 4.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible