【LeetCode笔记】Maximum Depth of Binary Tree 二叉树最大深度 递归&非递归

求二叉树的深度_递归方法和非递归方法(二叉树) 算法特点及适用场景递归方法简单直观,基于二叉树的递归定义直接计算深度,代码简洁,但对于非常深的二叉树可能存在栈溢出风险。适用于一般二叉树深度计算,尤其是二叉树结构不太复杂的情况。非递归方法利用层次遍历思想,避免了栈溢出问题,通过队列辅助计算深度,对于各种二叉树结构都能稳定计算深度。适用于可能存在深二叉树结构或者对栈空间有限制的场景,如在一些资源受限的系统中计算二叉树深度。注意事项及可能遇到的问题在递归方法中,要注意递归终止条件的正确设置,以及代码中可能出现的错误(如原代码中计算右子树深度时的错误写法)。 阅读详情

轻而易举的敲下了经典的递归程序:

/**
 * 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;
        else
            return maxDepth(root->left)>maxDepth(root->right)? maxDepth(root->left)+1:maxDepth(root->right)+1;
    }
};
然后系统告诉我,38个测试用例过了36个,然后

 Time Limit Exceeded
好吧,递归确实好用,但是占用的资源有点多,对于树很大的情况不好使。


非递归方法:

又让我想到了第二题!求树的最大深度呀->第二题用二维vector按层存储二叉树的时候,第二维的个数不就是层数嘛,层数不就是深度嘛!

同样的,使用queue先进先出,当上一层全部pop出去的时候,当前层刚好全部在里面。所以每得到一层,count++

/**
 * 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) {
        queue<TreeNode*> q;
        TreeNode* p;
        int count = 0;
        if (root == NULL)
            return 0;
        else{
            q.push(root);
            while (!q.empty()){
                int size = q.size();
                for (int i=0;i<size;i++){
                    p = q.front();  //注意取节点的代码要在第二层循环里面,因为每一个for循环全部执行完,
                    q.pop();        //就相当于拿走了上一层的全部节点,并存入了下一层的全部节点
                    if(p->left!=NULL)
                        q.push(p->left);
                    if (p->right!=NULL)
                        q.push(p->right);
                }
                count++;
            }
        }
        return count;
    }
};



鼎捷T100——二次开发:维护作业添加非实体字段 非实体字段添加 阅读详情

相关推荐

初识数据结构之入门必懂——线性表(顺序表)

本文将介绍数据结构中的线性表与线性表中最简单的结构——顺序表

m0_64705485的博客 905

图解LeetCode——662. 二叉树最大宽度(难度:中等)

对于编号的存储,我们可以创建一个对象,里面包含编号和TreeNode这两个变量,也可以使用JDK内置的Pair对象,由于本题中,节点的val值没有任何用处,所以,编号我就存储到了val属性值中,这样更易于存储和获取。构建虚拟的空节点虽然可以满足题目的计算逻辑,但是,由于要大量的创建空的虚拟节点,而且越到层级越深且该层级真是节点越少,那么创建的空节点将会非常的多,那么在提交的时候,就会。那么,由于没有子节点就创建空的虚拟节点,如果不添加某个判断条件,这种构建空节点的操作将会无限的创建下去。...

爪哇缪斯的博客 1459

adb详细教程(四)-使用adb启动应用、关闭应用、清空应用数据、获取设备已安装应用列表

adb对于安卓移动端来说,是个非常重要的调试工具。通过ADB可以操作、管理设备上的应用。

公众号|软件测试老吴 3万+

LeetCode104. 二叉树的最大深度(递归&非递归)

LeetCode104.二叉树的最大深度(递归与非递归解法)

weixin_42307601的博客 862

C++ 核电站

问题描述 一个核电站有n个放核物质的坑,排列在一条直线上。如果连续m个坑中放入核物质就会发生爆炸,所以某些坑中不能放核物质。 本题的任务:对于给定的n和m,求不发生爆炸的放置核物质的方案总数。 输入格式 第一行两个正整数n和m,之间用一个空格隔开。2≤n≤50,2≤m≤5。 输出格式 一行一个正整数,表示方案总数。 输入样例 4 3 输出样例 13 数据规模与约定 2≤n≤50,2≤m≤5 CODE #include&lt;bits/stdc++.h&gt; using namespace

HybridCOW_HORSE的博客 1803

基于FME—三调篇—尖锐角及小缝隙处理

1 问题分析: 该功能实现在FME较为简单,相对ARCGIS模型构建器以及ARCGIS ENGINE,FME更适合无代码基础的人群。在三调内业数据处理要求中,尖锐角是不允许存在的。本文基于FME实现了对尖锐角的自动查询与消除以及自动处理在消除尖锐角过程中产生的缝隙,并进行了拓扑检查,实现自动化批量处理尖锐角的功能,提高工作效率。 2 总体思路: 2.1 实现功能分析: 1.找出小于指定角度的尖锐角...

qq_33315579的博客 9180

非递归算法求二叉树的深度

原理: 1.采用层次遍历的方法, 2.设置变量level记录当前结点所在层数, 3.设置变量last指向当前层最右结点, 4.每次层次遍历出队时,与last指针作比较,若两者相等,那么层数加1, 并让last指向下一层最右结点,至少遍历完成.level的值即为二叉树的高度 #include &amp;lt;stdio.h&amp;gt; #include &amp;lt;stdlib.h&amp;gt; typedef 1000...

To be a better man 4115

从零到一:Quartus II连接开发板实战,打通绘图、管脚分配、编译与下载全链路

本文详细介绍了使用Quartus II软件连接FPGA开发板并完成首个项目的全流程。从硬件连接到驱动安装,到绘制电路框图、精准分配管脚,再到编译设置与最终下载,手把手指导您打通从设计到硬件实现的完整链路,是FPGA初学者的实战入门指南。

mqtt6iot的博客 717

非递归方法求解二叉树最大深度

题目 给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 说明:叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最大深度3 。 分析 使用递归,很容易写出该求解算法,思路也简单,就是左子树和右子树高度,取两者最大,然后在此基础上加1即可,如下图所示,差不多1到2行代码。 public in...

dinghua_xuexi的专栏 3044

宇树 Qmini 双足机器人完全开源:3D打印+5小时组装,新手也能玩转!

教育教学,成为学校里的竞赛竞技,实验课程,论文设计的本体;开发者可以根据开源资料轻松集齐所有零部件,结构件可全部通过3D打印完成(几乎完全不需要任何机加工零部件),搭配宇树的高可靠性电机和标准电池,客户打印完零部件后,大概仅需要3~5小时的手工组装即可得到一只可爱的Qmini小双足机器人本体。一起推动机器人的未来发展,创新没有边界,创造力是无限的,Qmini就是你的第一步,如果你也想做一个能动、能学、还能萌的小双足机器人,现在就可以动手了,全开源、真低门槛、只差你的3D打印机和一点点耐心。

雕爷学编程 1889

Python 爬虫实战:爬取 BOSS 直聘职位,实现岗位要求关键词提取

摘要:本文基于BOSS直聘数据分析师岗位信息,采用Python爬虫技术获取职位数据,通过文本分析和可视化方法研究岗位技能需求特征。研究首先设计爬虫程序获取职位基本信息和岗位要求文本,经数据清洗后运用jieba分词和词频统计提取关键词。结果显示SQL、Python为最核心技能,不同级别岗位需求差异明显:初级重基础数据处理,高级需建模和管理能力;行业间亦存在差异,互联网关注用户增长,金融侧重风控。研究为求职者技能提升提供了数据支持,同时展示了从非结构化文本中挖掘有价值信息的完整流程。

2503_91057718的博客 1766

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万+

Statistics and Machine Learning Toolbox

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

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

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

android 蓝牙通讯demo

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

leetcode 104. Maximum Depth of Binary Tree 解法 python

一.问题描述 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...

CSerwangjun的博客 571

[LeetCode]104. Maximum Depth of Binary Tree ★

每天一道编程题题目描述样例python解法C语言解法 题目描述 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 ...

xingyu97的博客 267

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

韩旭051的博客 525

LeetCode 104. Maximum Depth of Binary Tree 二叉树的最大深度

题目:两个stack的妙用

zhangzhetaojj的博客 200

leetcode 104. Maximum Depth of Binary Tree (求二叉树的最大深度,DFS,BFS)

题目要求 给定一个二叉树,求其最大深度。 最大深度:从根结点到叶子结点包含的节点的最大个数。 注意: 叶子结点说的是一个没有孩子的节点。 输入示例 Example: 输入一个二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its depth = 3. 解题思路 1、用深度优先遍历(DFS)...

爱喝舒化奶的博客 634

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

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

dianjige2532的博客 141

leetcode刷题104 二叉树的最大深度 Maximum Depth of Binary Tree(简单) Python Java

给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 说明:叶子节点是指没有子节点的节点。 示例: 给定二叉树[3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最大深度3 。 解题思路: 当root为空时,返回深度0. 当roo...

SpringRolls的博客 223

LeetCode 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.思路分析:这题和Maximum Depth of Binary Tree类似,但是现

Coding for Dreams 1534

leetcode【104】Maximum Depth of Binary Tree

问题描述: Share 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 w...

fanyuwgy的博客 127

[Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度

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. 递归法

hxm102581的博客 467

关于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.     题目内容也非常简单,给定一个二叉树的根结点

zsy112371的专栏 352

LeetCode 104. Maximum Depth of Binary Tree

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

user2639的博客 179
上一篇: 【LeetCode笔记】Binary Tree Zigzag Level Order Traversal 二叉树Z字形遍历
下一篇: 【LeetCode笔记】Construct Binary Tree from Preorder and Inorder Traversal 前序、中序还原二叉树
macidoo
博客等级 码龄11年 13粉丝 18原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值