应用题-二叉树专题-二叉树的最大深度
**写法1**
```python
# 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:
def maxDepth(self, root: Optional[TreeNode]) -> int:
self.max_h = 0
def maxDepth_( r, h ):
if not r: return
if h > self.max_h: self.max_h = h
maxDepth_( r.left, h + 1 )
maxDepth_( r.right, h + 1 )
maxDepth_( root, 1 )
return self.max_h
```
**写法2**
```python
# 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:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root: return 0
h1 = self.maxDepth( root.left )
h2 = self.maxDepth( root.right )
return max( h1, h2 ) + 1
```