LeetCode 104. Maximum Depth of Binary Tree

BGE(BAAI General Embedding)模型详解 BGE(BAAI General Embedding)是北京智源人工智能研究院(BAAI)推出的通用文本嵌入模型系列,旨在为各种自然语言处理任务提供高质量的向量表示。 阅读详情

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

Solution1:我的答案
反手就一个递归写法。。。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (!root) return 0;
        return 1 + max(maxDepth(root->left), maxDepth(root->right));
    }
};

举一反三:
求二叉树的最小深度链接如下:
http://www.cnblogs.com/grandyang/p/4042168.html
代码:
在求二叉树的最小深度时需要注意的一点是当二叉树的一个子树为空时,另一支非空子树需继续递归下去

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode *root) {
        if (root == NULL) return 0;
        if (root->left == NULL && root->right == NULL) return 1;

        if (root->left == NULL) return minDepth(root->right) + 1;
        else if (root->right == NULL) return minDepth(root->left) + 1;
        else return 1 + min(minDepth(root->left), minDepth(root->right));
    }

};
从 VS Code 登录 ABAP Destination,一次把前后端连接跑通 最近很多 ABAP 开发者开始把 VS Code 纳入日常开发环境,原因很直接,VS Code 的轻量体验、命令面板、文件树、插件生态和 AI 编程能力,已经深深影响了现代开发者的工作方式。SAP 官方发布的 ABAP Development Tools for VS Code,也把 ABAP Cloud 开发模型带进了这个编辑器里。 阅读详情

相关推荐

图像修复黑科技:对比BM3D/DnCNN等5种去噪器在PNP框架中的实战效果

本文深度评测了BM3D、DnCNN等五款主流去噪器在即插即用(PnP)优化框架中的实战表现。通过量化指标、视觉对比及超分等泛化任务测试,揭示了深度学习模型在性能与收敛速度上的优势,并为不同应用场景提供了选型指南与工程化建议。

weixin_29065959的博客 372

LeetCode 104. Maximum Depth of Binary Tree 题解

本题是二叉树的基本问题,主要考察对二叉树遍历和递归的理解。递归深度优先搜索:代码简洁易懂,但在树深度较大时可能会遇到栈溢出的问题。迭代深度优先搜索:避免了递归的栈溢出问题,适用于深度较大的树。广度优先搜索:层次清晰,易于理解,且在树宽度较大时表现较好。在实际应用中,可根据树的具体情况选择合适的方法。对于一般的二叉树,递归方法通常是最直观和简洁的选择。

cannonjinx的博客 4588

西门子200 SMART与博图WinCC通讯全攻略:从PC Access到S7-200协议避坑指南

本文详细解析了西门子S7-200 SMART PLC与博途WinCC上位机通讯的两种主流方案:基于PC Access SMART的OPC通讯和利用博途内置S7-200协议驱动直连。文章深入对比了两种架构的优缺点、配置步骤,并提供了从网络层到软件层的深度排错指南与性能优化建议,帮助工程师高效、稳定地实现系统集成。

gg901的博客 820

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.

老夫编代码就是一把梭 594

Python新手必看:5分钟搞定睿尔曼机械臂基础控制(附代码示例)

本文为Python新手提供了快速上手睿尔曼机械臂基础控制的实战指南。通过详细的步骤讲解和代码示例,介绍了从环境搭建、网络配置、SDK安装到建立连接、执行关节运动(movej)、直线运动(movel)等核心控制指令的全过程,帮助零基础开发者快速实现机械臂的基础操控。

hp777的博客 330

Apollo MPC横纵向耦合控制学习笔记

Apollo MPC横纵向控制原理学习笔记

weixin_39199083的博客 1万+

Leetcode 104 Maximum Depth of Binary Tree

leetcode104 题目描述 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. Note: A leaf is a node ...

qq_43271202的博客 234

Postman账号

Postman账号一、为什么要注册一个Postm账号二、如何注册Postman账号三、登录账号四、切换账号五、找回账号密码 一、为什么要注册一个Postm账号 注册Postman账号是免费的。当注册并登录一个Postman账号后,用户可以获得一下权限。 (1)同步和备份历史、集合、环境和预置头。 (2)可以轻松处理来自不同机器的多个Postman实例‘ (3)创建集合链接发送给其他开发人员。 当然,不注册也可以使用Postman。 二、如何注册Postman账号 (1)下载、安装Postman应用程序。 (

精益求精,永无止境,永远在路上! 6777

闯关leetcode——104. Maximum Depth of Binary Tree

最简单的办法就是遍历这棵树,对于每个节点,返回它的最长子树的深度+1。对于nullptr,返回0。这种题都是递归思想。

方亮的专栏 899

360加固DEX解密与ELF修复技术解析

本文深入解析了360加固APK的逆向分析技术,重点探讨了其DEX解密与ELF文件修复的核心流程。通过动态调试与Frida脚本,详细拆解了壳的启动机制、反调试绕过、自实现Linker技术,并还原了基于RC4和自定义算法的多层DEX解密过程,为移动安全研究人员提供了实用的实战分析思路。

alice7model的博客 704

GEPA_AIME教程:使用GEPA优化数学问题求解的完整指南

本教程展示了如何使用DSPy框架中的GEPA(Generative Evolutionary Prompt Adaptation)优化器来提升大语言模型在数学竞赛问题(AIME)上的表现。# 创建基础的链式思考程序"""带有详细反馈的评估指标,为GEPA提供学习信号"""try:# 处理解析错误feedback_text = f"最终答案必须是一个有效的整数。你回答了'

步子哥的博客 1387

LeetCode题解-104-Maximum Depth of Binary Tree

尽管写LeetCode解答主要是为了自己整理思路,但是如果对您也正好有帮助,请点个赞给菜鸟一点鼓励吧 :-) 原题 解法分析 解法1使用递归法; 解法2参考了别人的思路,为迭代法,类似于广度优先搜索; 自己的迭代法使用了后序遍历来统计树的深度,较为繁琐,没有参考意义。 解法1 解法分析 递归计算左子树的深度与右子树的深度,那么树的深度为两个子树中深度

WangT443的博客 632

【VTKExamples::PolyData】第十六期 GradientFilter

本文分享VTK样例GradientFilter,并解析接口vtkGradientFilter & vtkAssignAttribute,希望对各位小伙伴有所帮助!

qq_40041064的博客 322

【LeetCode】104. Maximum Depth of Binary Tree

我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的读书笔记!期待您的关注,欢迎一起学习交流进步! 知乎主页:https://www.zhihu.com/people/MicrostrongAI/activities Git...

Microstrong 583

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

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. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7],

qq_41704019的博客 329

精雕软件(JDpaint) 5.5 绿色版

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

Leetcode104. Maximum Depth of Binary Tree

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 nod...

magic_jiayu的博客 276

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

Leetcode 104. Maximum Depth of Binary Tree

Leetcode 104. Maximum Depth of Binary Tree 题目描述:找出二叉树的最大深度。 题目链接:Leetcode 104. Maximum Depth of Binary Tree 思路:递归的做法就是不断返回二叉树的左子树右子树深度比较大的那一个。 迭代的做法就是,用hashmap记录每个节点的深度,然后不断遍历的过程中更新ans,最后加上根节点的深度。 ...

Za七杂⑧ 218

LeetCode104—Maximum Depth of Binary Tree

LeetCode104—Maximum Depth of Binary Tree求二叉树的深度。

上善若水 860

leetcode【104】Maximum Depth of Binary Tree

写在最前面:null   leetcode【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 th...

ssjdoudou的博客 2万+

LeetCode力扣 104. 二叉树的最大深度 Maximum Depth of Binary Tree 题解代码 JavaScript

问题https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/submissions/ 练习使用JavaScript解答 /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {Tre.

Coding home - 漂流瓶jz 607

【leetcode c++】104 Maximum Depth of Binary Tree

Maximum Depth of Binary Tree   Given a binary tree, find its maximumdepth. The maximum depth is the number of nodesalong the longest path from the root node down to the farthest leaf node.   给一棵二

lif的博客 497

[LeetCode]104. Maximum Depth of Binary Tree

[LeetCode]104. Maximum Depth of Binary Tree题目描述思路深搜,节点为NULL返回0,其余比较左右子树大小,返回大值代码/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *ri

Lcharon的博客 2845

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. Note: A leaf is a node with no childr...

中华小当家的博客 261

LeetCode:104. Maximum Depth of Binary Tree

LeetCode: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 no...

Wisimer 188
上一篇: 【卡塔兰数】LeetCode 96. Unique Binary Search Trees
下一篇: 【重点 递归构造二叉树】LeetCode 95. Unique Binary Search Trees II
Allenlzcoder
博客等级 码龄10年 925粉丝 512原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值