Leetcode---Maximum Depth of Binary Tree

保姆级教程:用U盘给浪潮服务器装系统(含RAID驱动加载技巧) 本文提供了一份详细的浪潮服务器U盘安装系统指南,重点介绍了RAID驱动加载技巧与实战步骤。从准备工作到系统安装全流程,涵盖RAID配置、驱动加载方法及常见问题排查,帮助用户高效完成服务器系统部署。特别强调RAID驱动与服务器型号的匹配性,确保安装过程顺利。 阅读详情

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.


Show Tags
Have you met this question in a real interview?

求一棵树的高度,很显然,递归,三行代码。。。


  1. int maxDepth(TreeNode *root) {
  2.         if(root==NULL)
  3.             return 0;
  4.         return max(maxDepth(root->left),maxDepth(root->right))+1;
  5.     }


第二种做法:遍历

int level;
int result;
int maxDepth(TreeNode *root) {
    level++;
    if(root==NULL){
       result=max(result,level-1);
    }
    else{
        maxDepth(root->left);
        maxDepth(root->right);
    }
    level--;
    return result;
}


jre9下载 jdk9 (Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】 立即下载

相关推荐

Mit6.S081-实验1-Xv6 and Unix utilities

Mit6.S081-实验1-Xv6 and Unix utilities前言一、Boot xv61,实验目的2,操作流程1)切换到xv6-labs-2020代码库的lab1分支2)启动xv63)测试xv64)过程分析5)其他操作二、在xv6中添加一个自己编写的程序1,源码准备2,编译配置3,测试添加程序4,过程分析三、xv6中shell简析 前言 一、Boot xv6 1,实验目的 利用qemu启动xv6 2,操作流程 1)切换到xv6-labs-2020代码库的lab1分支 git checkout ut

code a better life~ 36万+

LeetCode 104. Maximum Depth of Binary Tree 题解

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

cannonjinx的博客 4588

深入解析:如何基于开源OpENer开发EtherNet/IP从站服务

通过OpENer开源库,开发者可以快速实现符合ODVA规范的EtherNet/IP从站设备,降低开发成本并提高系统灵活性。本文详细介绍了EtherNet/IP协议原理、OpENer库的特点与优缺点,以及二次开发的完整流程。通过遵循本文提供的指南,开发者可以成功构建出工业级的EtherNet/IP通信设备。

极地星辰 2999

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

实战复盘:如何用Frida脚本绕过某书APP的libmsaoaidsec.so检测(附完整Hook代码)

本文深入探讨了如何利用Frida脚本绕过某书APP通过libmsaoaidsec.so实现的反Frida检测机制。通过分析动态链接过程中的关键节点和检测模式,提供了两种实战验证的绕过方案:动态Hook关键链接器函数和静态Patch SO文件。文章还分享了进阶对抗策略和调试技巧,帮助安全研究人员有效应对现代移动安全防护挑战。

weixin_42706667的博客 218

STM32异常与中断过程详解

1. 异常与中断概念引入 异常主要是指来自CPU内部的意外事件,比如执行了未定义指令、算术溢出、除零运算等发生在CPU内部的意外事件,这些异常的发生,会引起CPU运行相应的异常处理程序;中断一般来自硬件(如片上外设、外部I/O输入等)发生的事件,当这些硬件产生中断信号时,CPU会暂停当前运行的程序,转而去处理相关硬件的中断服务程序。但无论是异常还是中断,都会引起程序执行偏离正常的流程,转而去执行异常/中断的处理函数。 下面对异常和中断的介绍,如果中断信号的产生原因来自CPU内部,则称之为异常;如果中断信号来

luobeihai的博客 1万+

LeetCode——Maximum Depth of Binary Tree

LeetCode——Maximum Depth of Binary Tree Question 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 lea...

weixin_30535843的博客 133

BLDC&PMSM电机控制器硬件设计工程(三)控制器驱动芯片方案

在汽车电机控制系统中,栅极驱动IC扮演着至关重要的角色。它作为主控MCU与功率开关管(MOSFET/IGBT)之间的桥梁,将微弱的PWM信号放大为足以快速、可靠驱动功率管的强信号。本文将深入解析英飞凌、TI和NXP的车规级栅极驱动方案,重点关注半桥和单侧驱动IC。1)为什么需要栅极驱动器?栅极驱动器是低压控制器和高压电路之间的缓冲电路,用于放大控制器的控制信号,从而实现功率器件更有效的导通和关断。栅极驱动器的作用总结如下:1.将控制器的低压信号转化为更高电压的驱动信号,以实现功率器件稳定导通和关断。

小幽余生不加糖 492

Maximum Depth of Binary Tree -- LeetCode

原题链接: http://oj.leetcode.com/problems/merge-two-sorted-lists/  这道题目比较简单,经典的链表基本操作。维护两个指针对应两个链表,因为一般会以一条链表为基准,比如说l1, 那么如果l1当期那的元素比较小,那么直接移动l1即可,否则将l2当前的元素插入到l1当前元素的前面。算法时间复杂度是O(m+n),m和n分别是两条链表的长度,空间复杂

Code Ganker 1万+

即插即用篇 | 手把手教你 YOLOv11 添加注意力机制 | 适用于【检测任务】【分类任务】【分割任务】【关键点任务】| 20+ 种全打通!

即插即用篇 | 手把手教你 YOLOv11 添加注意力机制 | 适用于【检测任务】【分类任务】【分割任务】【关键点任务】| 20+ 种全打通!

Coding... 2831

Android 蓝牙——概述(一)

Profiles (蓝牙应用规范),它从应用场景的角度为蓝牙技术的使用制定了不同的规范。这也是和大众日常生活接触最多的一部分。蓝牙支持很多Profiles:MAP:(MessageAccessProfile),短信访问协议,允许设备间的信息交换,短信。使用场景:智能车载中同步短信等信息。HFP:(Hands-free Profile),电话免提协议,让蓝牙设备(如蓝牙耳机/车机)可以控制电话,如接听、挂断、拒接等。

小旭的专栏 3492

LeetCode 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.思路分析:这题比较简单,关于树的题目通常都可以用递归解决,这题也不例外,递归解法的思

Coding for Dreams 1739

杰理SDK入门教程(一):SDK的四种编译方式的使用

杰理SDK提供了四种主要编译方式:1) JL Studio官方IDE,适合初学者;2) VS Code轻量级编辑器,需在SDK根目录打开;3) 命令行/脚本编译,灵活支持自动化;4) Code::Blocks开源IDE。每种方式都有详细操作指南,开发者可根据需求选择,初学者推荐从JL Studio开始熟悉编译流程。环境配置注意事项和编译成功标识均有明确说明,确保开发效率。

qq_42014563的博客 1149

【LeetCode】Maximum Depth of Binary Tree

题目描述 http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Maximum Depth of Binary Tree   Given a binary tree, find its maximum depth. The maximum depth is the number of

Snowwolf_Yang的程序化生活 673

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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

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

方亮的专栏 892

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

LeetCode 之 Maximum Depth of Binary Tree

最近看了点动态规划,二叉树的深度用递归方法解决,算得上是动态规划,不过由于二叉树比较简单,因此不需要应用带备份的算法,而且由于二叉树有方向,只能自顶向下,不能自底向上,说是动态规划,其实就是递归而已。。。。不过学了东西之后归类还是好的,而且在开始犯了一个2b的错误,为了简化代码下了如下错误的: class Solution { public: int maxDepth(TreeNod

zhao123h的专栏 768

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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

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 606

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

Leetcode Maximum Depth of Binary Tree

2015年7月6日。 Leetcode: 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 fa

程序人生写程序,又拿程序换酒钱。 783

[LeetCode] Maximum Depth of Binary Tree

前言LeetCode 的blabla Tree系列又一基础题,可配合 Minimum Depth of Binary Tree 一起练习。题解在此。题目https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maximum depth. The maximum depth

AllZone 381

LeetCode: 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. 即求二叉树的(最大)深度。 有两种思路:一是使用递归,二

给永远比拿愉快 1252

leetcode Maximum Depth of Binary Tree

解题思路: 深度优先遍历二叉树, 先遍历左孩子,一直到叶子节点,然后回溯,用栈来回溯,遍历右孩子。用栈表示哪些节点有被访问过。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; *

yueliangku的专栏 603

【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的博客 496

Leetcode之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 ch...

qq_35455503的博客 147
上一篇: Leetcode---Symmetric Tree
下一篇: Leetcode---Same Tree
我把葡萄酿成酒
博客等级 码龄12年 275粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值