水题,递归遍历childern,判断高度最大的那个返回即可。
代码如下:
"""
# 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
result = 1
for child in root.children:
temp = self.maxDepth(child)
if result < temp+1:
result =temp+1
return result