LeetCode 104. Maximum Depth of Binary Tree && Minimum Depth of Binary

【gcc】跨版本兼容实战:高版本编译程序在低版本glibc环境下的部署方案 本文针对高版本gcc编译的程序在低版本glibc环境下部署的兼容性问题,提供了四种实战解决方案:静态编译的局限性、容器化部署的优雅方案、使用Devtoolset工具链的官方支持,以及最通用灵活的打包依赖SO发布方案。重点剖析了通过设置RPATH和Interpreter或使用patchelf工具,实现程序与自带库的独立部署,有效解决glibc版本鸿沟。 阅读详情

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

本题目是简单的二叉树深度问题,二叉树是每个节点最多有两个子树的树结构,二叉树还有递归定义:

二叉树是n(n>=0)个有限结点构成的集合。N=0称为空二叉树;n>0的二叉树由一个根结点和两互不相交的,分别称为左子树和右子树的二叉树构成。

二叉树中任何结点的第1个子树称为其左子树,左子树的根称为该结点的左孩子;二叉树中任何结点的第2个子树称为其右子树,左子树的根称为该结点的右孩子。

更多二叉树的性质和实现:见luoweifu的博客 二叉树(1)——二叉树的定义和递归实现

利用递归求二叉树的最大深度:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int maxDepth(TreeNode root) {
        
        if(root == null){
            return 0;
        }
        
        int leftMaxDepth = maxDepth(root.left);
        int rightMaxDepth = maxDepth(root.right);
        return 1 + Math.max(leftMaxDepth,rightMaxDepth);
    }
}

简单解释:将每个结点都看做根节点,如果结点为空,则返回0,此为递归的出口,如果不为空,则深度至少为1,分别求其左子树和右子树的深度,进行比较,即可求出最大深度。

最近看到了求二叉树最小深度:

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

和求二叉树的最大深度不同,最小深度的计算需要考虑更多,当根结点只有左子树或者右子树,其深度的计算不能包含另一部分的计算:

代码如下:

public class Solution {
    public int run(TreeNode root) {
        if(root==null)
            return 0;
        if(root.left==null&&root.right==null)
            return 1;
        if(root.left==null)<span style="line-height: 25.2px; font-family: arial, STHeiti, "Microsoft YaHei", 宋体;">// 当前结点左子树为空,只有右子树</span>
            return run(root.right)+1;
        if(root.right==null)// 当前结点右子树为空,只有左子树
            return run(root.left)+1;
        return Math.min(run(root.left),run(root.right))+1;
    }
}


使用 Ansys Q3D 进行电容提取 精确的电容提取在高速和 RF 设计中至关重要。在本博客中,我们演示了如何使用Ansys Q3D Extractor来计算电容值,从基本的平行板电容器开始,然后转到MEMS芯片的切口,以检查焊盘和RF线对整体电容的影响。在下面的比较中,我们使用 Q3D 从 MEMS 芯片切口中提取两种配置的电容矩阵:一种仅带有焊盘,另一种带有连接到 RF 线路的焊盘。这种比较表明,焊盘是总电容的主要贡献者,而 RF 线路则略有增加。按照上述工作流程,提取焊盘的电容,得到的电容矩阵如下所示。,选择一个已识别的网络,然后单击。 阅读详情

相关推荐

AI 赋能测试全生命周期:多 Agent 协同的自动化提效方案

本文提出了一种基于Coze平台的多Agent协同测试解决方案,旨在解决软件测试中PRD解析不清晰、测试点覆盖不全、用例编写效率低、代码生成耗时等痛点。通过PRD优化、测试点生成、测试用例生成和单元测试框架生成四个Agent的协同工作,结合Cursor工具实现从需求到代码的全链路自动化。以电商平台登录模块为例,展示了从模糊PRD到标准化测试用例和可执行Java单元测试代码的完整转化流程。该方案通过AI赋能显著提升测试效率,减少人工干预,实现测试覆盖率≥95%,为快速迭代的软件研发提供了高效的测试自动化支持。

QINJIEEE的博客 1124

LeetCode 104. Maximum Depth of Binary Tree和111. Minimum Depth of Binary Tree 递归

104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

老夫编代码就是一把梭 596

2023东华大学机试OJ进阶版1-81题

先排序。每次找两个最小的数,把其中一个置为两数之和,并累加这个和值,另一个数置为-1,然后再排序,重复操作,直到只剩下一个数。105。

m0_49263811的博客 2810

Minimum Depth of Binary Tree -- LeetCode

原题链接:http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/  这道题是树的题目,其实跟Maximum Depth of Binary Tree非常类似,只是这道题因为是判断最小深度,所以必须增加一个叶子的判断(因为如果一个节点如果只有左子树或者右子树,我们不能取它左右子树中小的作为深度,因为那样会是0,我们只有在叶子节

Code Ganker 1万+

esp32-C3固件烧录用户手册

温馨提示:1.esp32-C3 BOOT脚为GPIO9,需要从芯片小板中焊接引出2.芯片小板中已经预留了所有烧录相关的引脚,大家可以按需焊接引线3.程序下载完,使用串口查看log时,需要使用220v供电使用(请务必准守用电安全)。原因:由于插座中包含继电器,计量计等耗电设备,所以usb转串口提供的3.3V电压和电流可能会出现供电不足,表现为usb转串口与电脑连接断断续续,导致无法查看log。

hbwang的博客 1万+

浅谈迪杰斯特拉(Dijkstra)算法和A*算法原理及实现

迪杰斯特拉(Dijkstra)算法和A*算法的原理及实现。

Guo_Zhanyu 9613

leetcode 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree

104: class Solution { public: int maxDepth(TreeNode* root) { if(root == NULL) return 0; int left = maxDepth(root->left); int right = maxDepth(root-&...

weixin_30507269的博客 112

【华为OD机考】2025C+B+2024E+D卷真题【完全原创题解 | 详细考点分类 | 不断更新题目 | 六种主流语言Py+Java+Cpp+C+Js+Go】

可上练习华子OD、大厂真题绿色聊天软件戳od1441了解算法冲刺训练(备注【CSDN】否则不通过)

weixin_48157259的博客 1万+

LeetCode DAY14(104. Maximum Depth of Binary Tree&111. Minimum Depth of Binary Tree)

【代码】LeetCode DAY14(104. Maximum Depth of Binary Tree&111. Minimum Depth of Binary Tree)

weixin_48078699的博客 169

分布式优化 (Distributed Optimization) 算法详解及案例分析

分布式优化是一种将优化问题分解为多个子问题并在多个计算节点上并行求解的方法。其目标是通过分布式计算资源高效地求解大规模优化问题。

闲人编程的博客 2735

GeoServer改造Springboot源码一(公共部分)

今天开始开启关于GeoServer的一个全新系列,主要是把改造Springboot后的详细代码粘贴出来,此文应配合《》系列共同阅读,按照前系列的时间顺序结合此系列的源码展示可以快速构建GeoServer功能的二次封装的后端系统。

龙井的专栏 1906

[LeetCode] 111. Minimum Depth of Binary Tree

原题链接: https://leetcode.com/problems/minimum-depth-of-binary-tree/ 1. 题目介绍 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root no...

Ber03的博客 177

华南理工大学-大学物理实验-不良导体热导率测量

不良导体热导率测量 引言 导热系数(又叫热导率)是反映材料热性能的重要物理量。热传导是热交换的三种(热传导、对流和辐射)基本形式之一,是工程热物理、材料科学、固体物理及能源、环保等各个研究领域的课题。材料的导热机理在很大程度上取决于它的微观结构,热量的传递依靠原子、分子围绕平衡位置的振动以及自由电子的迁移。在金属中电子流起支配作用,在绝缘体和大部分半导体中则以晶格振动起主导作用。因此,某种材料的导热系数不仅与构成材料的物质种类密切相关,而且还与材料的微观结构、温度、压力及杂质含量相联系。在科学实验和工程设计

mohen_777的博客 885

【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

题目分析: 给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。说明: 叶子节点是指没有子节点的节点。 示例:给定二叉树 [3,9,20,null,null,15,7], 返回它的最小深度 2. 解题思路: 与【LeetCode】104. Maximum Depth of Binary Tree 一致,把 【LeetCode】104. Maximum Dep...

L141210113的专栏 572

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

111. Minimum Depth of Binary Tree

文章目录111. Minimum Depth of Binary TreeDescriptionSolutionsSubmissionsSummary 111. Minimum Depth of Binary Tree https://leetcode.com/problems/minimum-depth-of-binary-tree/ Description Given a binary tre...

La La Land 165

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

LeetCode题解-111-Minimum Depth of Binary Tree

原题 链接:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 解题概览 需要注意的是这里的最小深度指的是根节点到叶节点的深度,例如对于二叉树[1, 2],最小深度为2而不是1! 解法1:递归; 解法2:广度优先遍历 解法1 解题思路 最小深度为左右子树的最小深度中较小的那个值+1 代

WangT443的博客 706

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

leetcode104---Maximum Depth of Binary Tree

问题描述: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.方法一:深度优先搜索!/** * Definition for a b

爱橙子的OK绷的专栏 444

LeetCode 111 Minimum Depth of Binary Tree

题目描述Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.分析参考:LeetCode 104 Maximum Depth of Bina

Yano_nankai的博客 426

LeetCode: 111. Minimum Depth of Binary Tree

题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree in which the depth of the two subtrees of every node never diff...

liveway6的博客 135

LeetCode 111 Minimum Depth of Binary Tree(DFS)

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题目大意:给出一个二叉树,求从根节点到叶节点的最小深度。 解题思路:直接D

nudt_oys的博客 692

LeetCode(111&104):二叉树的最小深度+最大深度 Minimum or Maximum Depth of Binary Tree(Java)

2019.4.3#程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) 本期开始更LeetCode,因为博客速度已经远远赶不上刷题速度,因此考虑只把比较有价值的题目放上博客作为记录。 深度就是按层计数的结点数,一个结点的深度是1。 二叉树的最小深度可以依靠剪枝层序遍历,也就是从根节点开始进行BFS,当遇到第一个叶子结点的时候停止,此时的深度对应着最小深度。 二叉树的最大...

NJU_ChopinXBP的博客 501

Leetcode111: Minimum Depth of Binary Tree(二叉树最小深度)

先看Leetcode104:Maximum Depth of Binary Tree,求二叉树最大深度。有两种解法,基于DFS(深度优先搜索)思想的方法使用递归计算:class Solution { public: int maxDepth(TreeNode *root) { if(root == nullptr) return 0; ...

Aaron_1934的博客 315

LeetCode—111. Minimum Depth of Binary Tree & 104. Maximum Depth of Binary Tree

LeetCode—111. Minimum Depth of Binary Tree 题目 https://leetcode.com/problems/minimum-depth-of-binary-tree/description/ 二叉树的最小深度。 思路及解法 递归 这道题其实需要想明白有哪几种情况: 父节点为空,深度是0 父节点不为空时,最小深度不一定就是两棵树中较小的深度,有可能一棵...

pnnngchg的博客 209

【刷题日记】leetcode——Minimum Depth andMaximum Depth of Binary Tree

题号104. Maximum Depth of Binary Tree 这个其实就是求树的深度,写一个递归就好了 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # ...

marvellousxf的博客 172

Leetcode 111,104. Minimum/Maximum Depth of Binary Tree

邻近毕业季, 关于算法的一些东西还是需要回顾一下的, 于是, 这里开始重新刷下Leetcode,巩固一下算法基础 题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node d

zhyh1435589631的专栏 591

LeetCode(111)Minimum Depth of Binary Tree

题目如下: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题目分析: 挺有意思的一道题目,corner cased a

BUILD 1853
上一篇: LeetCode 258. Add Digits
下一篇: LeetCode 237. Delete Node in a Linked List
Appletable
博客等级 码龄14年 0粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值