- 如果根节点为空,则深度为0,返回0,递归的出口
- 如果根节点不为空,那么深度至少为1,然后我们求他们左右子树的深度
- 比较左右子树深度值,返回较大的那一个
- 通过递归调用
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
@classmethod
def maxDepth(self, root):
if root == None:
return 0
else:
left = 1
right = 1
left = left + Solution.maxDepth(root.left)
right = right + Solution.maxDepth(root.right)
return max(left, right)