"""
# Definition for a Node.
class Node:
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution:
def maxDepth(self, root: 'Node') -> int:
def getMaxdeep(root, deep, max):
#if len(root.children) == None:
if len(root.children) == 0:
print(deep)
if deep > max:
max = deep
return max
for i in range(len(root.children)):
max = getMaxdeep(root.children[i], deep+1, max)
# deep-=1 ,每次循环deep都会自动回溯,因此不用再此处回溯
return max
max = 0
if not root:
return 0
return getMaxdeep(root, 1, max)
leetcode 559. Maximum Depth of N-ary Tree
最新推荐文章于 2021-04-20 19:01:01 发布