Back to course home
0% completed
Vote For New Content
Maximum Depth of N-ary Tree (easy)
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]
- Expected Output:
3
- Justification: The path from the root node
1
to the farthest leaf node5
goes through nodes1 -> 2 -> 5
, making the depth3
.
Example 2:
- Input: root =
[1,null,2,3,4,null,5,null,6,7,8,null,9,10,null,11,12]
- Expected Output:
4
- Justification: The longest path from the root node
1
to the farthest leaf node11
or12
is1 -> 2 -> 5 -> 12
, resulting in a depth of4
.
Example 3:
- Input: root =
[10,null,20,30,null,40,null,50,null,60]
- Expected Output:
4
- Justification: The path from the root node
10
to the farthest leaf node60
is10 -> 20 -> 40 -> 60
, making the depth4
.
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