原题
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/
解法
递归. Base case是当节点为空或者节点为叶子节点时, 分别返回0和1.
代码
"""
# 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
"""
# base case
if not root: return 0
if not root.children:
return 1
return 1 + max([self.maxDepth(node) for node in root.children])