Problem#104 Maximum Depth of Binary Tree

Problem

在这里插入图片描述

Solution

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
# 法一:递归方式
class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root == None:
            return 0
        if root.left == None and root.right == None:
            return 1
        left = self.maxDepth(root.left)
        right = self.maxDepth(root.right)
        return left + 1 if left > right else right + 1

猜你喜欢

转载自blog.csdn.net/gaolijing_/article/details/104701558