1014 B
1014 B
layout, title, last_solved, category, leetcode_url, status
| layout | title | last_solved | category | leetcode_url | status |
|---|---|---|---|---|---|
| page | Maximum Depth of N-ary Tree | 2020-07-23 | trees | https://leetcode.com/problems/maximum-depth-of-n-ary-tree/ | Solved |
Problem
Given a n-ary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
Example 1:
Input: root = [1,null,3,2,4,null,5,6]
Output: 3
Solution
{% highlight python %}
def maxDepth(self, root: 'Node') -> int:
if not root:
return 0
maxDepthAtThisRoot = 0
if root.children:
for child in root.children:
maxDepthAtThisRoot = max(maxDepthAtThisRoot, self.maxDepth(child))
return maxDepthAtThisRoot+1
{% endhighlight %}
