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