LeetCode104-二叉树的最大深度

原题链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/

解题思路:递归

代码:

 1 # Definition for a binary tree node.
 2 # class TreeNode:
 3 #     def __init__(self, val=0, left=None, right=None):
 4 #         self.val = val
 5 #         self.left = left
 6 #         self.right = right
 7 class Solution:
 8     def maxDepth(self, root: Optional[TreeNode]) -> int:
 9         if not root:
10             return 0
11         return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))

 

上一篇:第十六天104. 二叉树的最大深度


下一篇:2021-09-27(剑指 Offer 55 - I. 二叉树的深度)