leetcode104---Maximum Depth of Binary Tree

UE5与Cesium for Unreal结合:打造高效离线地图解决方案 本文详细介绍了如何利用UE5与Cesium for Unreal结合打造高效离线地图解决方案,特别适用于数字孪生、军事仿真等网络不稳定场景。通过优化配置、数据处理和性能调优,实现TB级地形数据的流畅渲染,并结合实战案例展示了在油田巡检等复杂环境中的应用效果。 阅读详情

问题描述:
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 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 == NULL) return 0;
        int left = maxDepth(root->left);
        int right = maxDepth(root->right);
        return max(left, right)+1;
    }
};

方法二:广度优先搜索!

class Solution {
public:
    int maxDepth(TreeNode* root) {
        return BFSsolution(root);
    }
    int BFSsolution(TreeNode *root){
    if(root == NULL) return 0;
    queue<TreeNode *> que;
    que.push(root);
    int depth = 0, count;
    TreeNode *left, *right;
    while(que.empty() == false){
        depth++;
        count = que.size();
        for(int i = 0; i < count; i++){
            left = que.front()->left;
            if(left) que.push(left);
            right = que.front()->right;
            if(right) que.push(right);
            que.pop();
        }
    }
    return depth;
}
};
MATLAB数据可视化:pcolor函数从入门到精通(附colormap调色秘籍) 本文深入解析MATLAB中pcolor函数的数据可视化应用,从核心机制、坐标网格指定到常见陷阱规避。重点揭秘colormap调色秘籍,涵盖内置色系选用原则、个性化定制方法及高级渲染技巧,助你创作出信息准确、视觉出色的专业图表,精通从数据到出版级图表的全流程。 阅读详情

相关推荐

逆向解析喜马拉雅新版xm-sign算法:从dws.1.6.8.js到签名生成

本文详细解析了喜马拉雅新版xm-sign算法的逆向破解方法。通过定位核心JS文件dws.1.6.8.js,构建本地Node.js模拟执行环境,并解密关键参数cid与appKey,最终实现签名算法的本地化稳定生成,为数据采集提供可靠解决方案。

weixin_29250403的博客 227

[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. 给一个二叉树,找出它的最小深度。最小深度是从根节点向下到最近的叶节点的最短路...

weixin_30347335的博客 198

CANoe信号传输故障排查指南

本文提供了全面的CANoe信号传输故障排查指南,涵盖从硬件连接、通道配置到软件脚本与数据库加载的全链路诊断。针对常见的通道指示灯不亮、信号无法发送及ECU握手失败等问题,给出了基于CANoe工具和CAPL脚本的详细排查步骤与解决方案,帮助工程师系统化定位并解决实车测试中的通信故障。

ice55的博客 270

[leetcode] 111. Minimum Depth of Binary Tree

找最小深度,要求叶子节点(即没有左右子节点的节点)到根节点的距离最短。递归遍历左右子树的最短长度,如果左子树为空了,就返回右子树的长度。

xBlg 222

工程经验分享:PCIE(1)

本文介绍AXI Bridge For Pcie Express Gen3 Subsystem 的使用方法。这个IP兼具简单与实用性,将PCIE协议层实现于内部,仅留出AXI接口供开发者使用。开发者不需要处理复杂的PCIE协议数据,只需要对AXI总线有所了解即可,十分符合Xilinx一贯的开发风格。本文无关PCIE协议本身,仅分享实用性的工程经验,以PCIE IP为核心,一步一步搭建一个简单的子系统,以此说明用法。文中所有内容均已通过项目检验。

weixin_42415266的博客 3855

华为手环实时心率数据导出到UWP项目中

hello大家好呀O(∩_∩)O最近在做毕设 其中有个小功能要用到实时心率数据。查找网上的资料发现只有小米手环的实时心率数据导出但是没有华为手环的,我试了华为手环但是并不能行得通哈哈哈。现在分享一下华为手环实时心率数据导出的代码。

apple_63713858的博客 1598

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

一文就够-InfiniBand 技术全面解析:从原理到实践的通俗易懂指南

想象一下,在一个超级数据中心里,成千上万台服务器需要像蜜蜂群体一样高效协作。如果用传统的网络连接,就像让每只蜜蜂都排着队传递消息,效率极低。InfiniBand(简称 IB)就是为了解决这种 "通信拥堵" 而生的高速网络技术,它专门为高性能计算(HPC)、数据中心和云计算环境设计,就像给服务器之间搭建了多条高速公路,让数据可以飞驰而过。

jundao1997的专栏 1414

【LeetCode】111. Minimum Depth of Binary Tree

两种解法: (1)深度优先遍历,与maximum depth of binary tree类似,时间复杂度,递归函数栈结构空间复杂度,但需要考虑两种情况: i. 节点返回时必须为叶子节点,返回深度应该为1。 ii. 为了满足i.实现时要返回非空子树的最小深度。 (2)广度优先遍历,优势在于,深度优先遍历会遍历全部子树,这在树非常不平衡时效率比较低。可以采用广度优先遍历来克服...

weixin_42344158的博客 257

基于Simulink的PID控制器自动驾驶仿真建模示例

本文通过Simulink仿真平台,详细讲解了基于PID控制器的自动驾驶路径跟踪实现方法。主要内容包括:1)建立自行车运动学模型作为被控对象;2)定义参考路径与横向误差计算方法;3)设计PD控制器并调试参数;4)在Simulink中搭建完整仿真模型;5)分析30km/h车速下的双移线跟踪性能。文章强调PID控制作为自动驾驶入门基础的重要性,并指出其在中低速场景下的适用性,同时给出了工程实践中的优化建议(如前视距离、抗积分饱和等)和扩展方向(如Stanley控制器)。通过完整案例,帮助读者掌握车辆运动学建模和P

xiaoheshang_123的博客 72

【ROS机器人系统】实验4 ROS简单应用:激光SLAM与导航

ROS简单应用:激光SLAM与导航 (1)了解机器人和SLAM之间关系 (2)了解激光SLAM (3)了解gmapping和导航

南京大学软件工程硕士在读 2157

111. Minimum Depth of Binary Tree [easy] (Python)

题目链接https://leetcode.com/problems/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 no

Coder_Orz的博客 3900

Python学习第32天:Web前端入门

本文介绍了Web前端开发三大核心技术:HTML、CSS和JavaScript。首先概述了HTML的发展历程和HTML5新特性,详细讲解了常用HTML标签如文本、列表、链接、图像、表格、表单等。其次介绍了CSS的基础语法、选择器、颜色字体设置、盒子模型和布局定位方法。最后简要提及JavaScript为网页添加交互功能的作用。文章为Python开发者提供了前端技术的基础知识,帮助理解网页的结构、样式和行为实现原理。

tigerhhzz的博客 1923

LeetCode 104. Maximum Depth of Binary Tree 题解

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

cannonjinx的博客 4591

visio安装包

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

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

精雕软件(JDpaint) 5.5 绿色版

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

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

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

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

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

方亮的专栏 903

LeetCode题解-104-Maximum Depth of Binary Tree

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

WangT443的博客 633

【LeetCode】104. Maximum Depth of Binary Tree

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

Microstrong 586

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的博客 330

LeetCode 104. Maximum Depth of Binary Tree

LeetCode 104. Maximum Depth of Binary Tree (Easy) 主要知识点:树、BFS、DFS、递归、迭代;优先级:5

温迪今天刷题了吗 445

Leetcode 104. Maximum Depth of Binary Tree

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

Za七杂⑧ 219

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

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 613

LeetCode104—Maximum Depth of Binary Tree

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

上善若水 861

【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的博客 498
上一篇: 工厂三兄弟之抽象工厂模式(一):界面皮肤库的初始设计
下一篇: 工厂三兄弟之抽象工厂模式(二):产品等级结构与产品族
爱橙子的OK绷
博客等级 码龄15年 351粉丝 329原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值