首页 > 其他分享 >104. 二叉树的最大深度

104. 二叉树的最大深度

时间:2023-01-28 15:45:44浏览次数:42  
标签:right self 二叉树 深度 root 104 left

问题描述

https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/

解题思路

二叉树的最大深度,等于左子树的深度和右子树深度的较大值加1(即本层深度).

代码

# 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 root is None:
            return 0
        return max(self.maxDepth(root.left), self.maxDepth(root.right))+1

 

标签:right,self,二叉树,深度,root,104,left
From: https://www.cnblogs.com/bjfu-vth/p/17070415.html

相关文章