LeetCode Minimum Depth of Binary Tree 找最小深度(返回最小深度)
题意:找到离根结点最近的叶子结点的那一层(设同一层上的结点与根结点的距离相等),返回它所在的层数。
方法有:
1、递归深度搜索
2、层次搜索
方法一:递归(无优化)
/**
* 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== ) //针对树根
return ;
if(root->left==&&root->right==) //是叶子,立即返回1
return ;
int zuo,you,com;
if(root->left!=&&root->right!=){ //左右孩子均存在
zuo=minDepth(root->left);
you=minDepth(root->right);
return zuo<=you?++zuo:++you;
}
else{
if(root->left!=&&root->right==){ //只有左孩子,那么只需要沿着左孩子方向搜索
com=minDepth(root->left);
}
else{ //即(root->left==0&&root->right!=0) 只有右孩子,那么只需要沿着左孩子方向搜索
com=minDepth(root->right);
}
return ++com;
}
}
};
思路:递归寻找叶子结点。
注意:如果一个结点只有一个孩子,不能调用两次函数来分别搜索左右子树,不然空的一边会返回0,那么结果就错了。
方法二:递归(稍微优化)
/**
* 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 mini(TreeNode *root) {
if( root->left== && root->right== ) //叶子结点,返回
return ;
int zuo,you,com;
if(root->left!= && root->right!= ){ //左右孩子均存在
if( root->left->left== && root->left->right== ){ //左孩子是叶子,右孩子不一定是叶子,无必要再搜索右孩子了
zuo=mini( root->left );
return ++zuo;
}
else if( root->right->left== && root->right->right== ){ //右孩子是叶子,左孩子不一定是叶子,无必要再搜索左孩子了
you=mini( root->right );
return ++you;
}
else{ //左右孩子均不是叶子,都需要继续向下搜索
zuo=mini( root->left );
you=mini( root->right );
return zuo<=you?++zuo:++you;
}
}
else{ //只有一个孩子
if(root->left!=) //只有左孩子
com=mini( root->left );
else //只有右孩子
com=mini( root->right );
return ++com;
}
}
int minDepth(TreeNode *root) {
if( root== ) //只针对空树
return ;
return mini(root);
}
};
思想是:若一个结点有两个孩子,而其中一个孩子是叶子,而另一个不是,那么就无需再搜索那个“不是叶子”的孩子了,因为它算出来的深度肯定比那个叶子的长。(具体自己画图领会)
吐槽:只是优化这么一点就需要考虑挺多东西。怪自己整体把握能力偏弱,总是需要先把代码打出来,修改,再修改才能解决。若能做到还没打代码之前就大概掌控整体结构,打出来就快多了。
方法三:层次遍历(暂时没空写)
LeetCode Minimum Depth of Binary Tree 找最小深度(返回最小深度)的更多相关文章
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LeetCode: Minimum Depth of Binary Tree 解题报告
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] Minimum Depth of Binary Tree 二叉树最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- LeetCode - Minimum Depth of Binary Tree
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- leetcode:Minimum Depth of Binary Tree【Python版】
1.类中递归调用添加self: 2.root为None,返回0 3.root不为None,root左右孩子为None,返回1 4.返回l和r最小深度,l和r初始为极大值: # Definition f ...
- leetcode Minimum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- Minimum Depth of Binary Tree,求树的最小深度
算法分析:递归和非递归两种方法. public class MinimumDepthofBinaryTree { //递归,树的最小深度,就是它左右子树的最小深度的最小值+1 public int m ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
随机推荐
- 深入浅出HTML PDF扫描版
<深入浅出HTML>是一部讲述现代Web标准的优秀教程,彻底摒弃了过时的内容,始终贯彻三层分离的思想.书中结合实例讲述如何使用HTML.CSS设计符合现代Web标准的网页,并讲解了如何使用 ...
- 远程桌面连接KVM虚拟机
问题描述 有些时候,由于网络存在问题,虚拟机无法获取到IP地址,无法使用spice或vnc来连接虚拟机,但是又需要连到虚拟机来排查故障 解决办法 编辑虚拟机配置 设置xml命名空间 <domai ...
- 如何从git上clone一个项目
今天想从自己的git上down下来代码,补充一些新的学习demo,不过因为平时工作中不适用git管理代码,所以,有些命令行忘记了.现在,通过这种方式再加深一遍印象吧. 那我就假设已经安装好了git了. ...
- java中pojo对象首字母大写导致无法赋值问题
命名规范(文末附有java命名规范)中指出,属性变量命名应采用驼峰命名的方式,即首字母小写,其他单词首字母大写: 但有时候我们对接三方的接口时,想要封装实体类来接受,但是发现接收到的参数的变量首字母是 ...
- 帝都Day4(3)——还是数据结构
可并堆 左偏树中 dist[x]=dist[rs[x]]+1 合并的时候,把权志较大的根作为根节点,把这棵树右子树和另一棵树合并. 说明白点:(上图描述有点问题) 设x表示根权值较大的左偏树,y表示根 ...
- BestCoder Round #86 1001
链接http://acm.hdu.edu.cn/showproblem.php?pid=5804 题意:给你一些商店和他的商品价格,然后给你一个记账本,问你记大了就是1,否则是0 解法:模拟,注意测试 ...
- 深刻理解Linux进程间通信(IPC)
https://www.ibm.com/developerworks/cn/linux/l-ipc/ linux下进程间通信的几种主要手段简介: 管道(Pipe)及有名管道(named pipe):管 ...
- python3+Appium自动化06-屏幕截图
截图方法 save_screenshot() 该方法直接保存当前屏幕截图到当前脚本所在文件位置 driver.save_screenshot('login.png') get_screenshot_a ...
- Linux与DOS的常用命令比较
命令类型 DOS Linux DOS示例 Linux示例 复制文件 copy cp copy c:\teacher1\file1 d:\tmp cp /home/teacher1/file1 /t ...
- elasticsearch.yml基本配置说明
一.基本配置 elasticsearch的config文件夹里面有两个配置文 件:elasticsearch.yml和logging.yml,第一个是es的基本配置文件,第二个是日志配置文件,es也是 ...