104. 二叉树的最大深度.md

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
left_high,right_high = None,None
def maxDepth(self, root: Optional[TreeNode]) -> int:
# 用后序遍历 求 高度。
if root==None:
return 0
left_high = self.maxDepth(root.left)
right_high = self.maxDepth(root.right)
return max(left_high,right_high) + 1