力扣 104. 二叉树的最大深度

前置

1
2
3
4
5
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

dfs

1
2
3
4
5
6
7
def maxDepth(root):
# dfs
def dfs(node):
if not node:
return 0
return max(dfs(node.left), dfs(node.right)) + 1
return dfs(root)
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信