题目
给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
代码
小结:参考二叉树的最大深度,注意root不为空的时候二叉树的深度是1,不是0。
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def maxDepth(self, root):
"""
:type root: Node
:rtype: int
"""
if root == None:
return 0
max_deep = 1
for child in root.children:
max_deep = max(max_deep, 1 + self.maxDepth(child))
return max_deep