Skip to content

Latest commit

 

History

History
34 lines (25 loc) · 576 Bytes

559.md

File metadata and controls

34 lines (25 loc) · 576 Bytes

559 Maximum Depth of N-ary Tree

Description

link


Solution

  • See Code

Code

最坏情况:O(n^2)

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""
class Solution:
    def maxDepth(self, root: 'Node') -> int:
        if not root:
            return 0
        if not root.children:
            return 1
        return max(self.maxDepth(x) for x in root.children) + 1