题目
给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
例如,给定一个 3叉树 :
我们应返回其最大深度,3
。
方案一(递归)
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
def maxDepth(root: 'Node') -> int:
if not root:
return 0
if not root.children:
return 1
depth = []
for child in root.children:
depth.append(self.maxDepth(child))
return max(depth) + 1
原文
https://leetcode-cn.com/explore/learn/card/n-ary-tree/160/recursion/624/