104. 二叉树的最大深度



# 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):
    # 递归思路:
    # (1)如果二叉树为空,二叉树的深度为0
    # (2)如果二叉树不为空,二叉树的深度 = max(左子树深度, 右子树深度) + 1
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        depthLeft = self.maxDepth(root.left)
        depthRight = self.maxDepth(root.right)
        return depthLeft + 1 if depthLeft > depthRight else depthRight + 1

posted @ 2020-08-30 16:26  人间烟火地三鲜  阅读(170)  评论(0编辑  收藏  举报