题目描述:
题解:递归
题目中节点的子节点用list表示。
1.递归终止条件:输入root节点为空,返回0的深度,如果root不存在子节点,返回1的深度。
2.递归返回值:当前节点的最大深度。
3.当前次递归:当前节点最大深度=所有子节点中的最大深度+1.
class Solution(object): def maxDepth(self, root): depth = 0 if root==None: return 0 if not root.children: return 1 return max(self.maxDepth(child)+1 for child in root.children)