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种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
随机推荐
- 字符串中去除多余的空格保留一个(MS SQL Server)
大约2年前,写过一篇<字符串中去除多余的空格保留一个(C#)>https://www.cnblogs.com/insus/p/7954151.html 今天,Insus.NET使用MS S ...
- 前端编码规范 -- css篇
合理的避免使用ID 一般情况下ID不应该被应用于样式. ID的样式不能被复用并且每个页面中你只能使用一次ID. 使用ID唯一有效的是确定网页或整个站点中的位置. 尽管如此,你应该始终考虑使用class ...
- Educational Codeforces Round 57D(DP,思维)
#include<bits/stdc++.h>using namespace std;char s[100007];long long a[100007];long long dp[100 ...
- d190305面试01过程记录和总结(java开发)
1.签到,做题,注意时间.(疑问:做笔试题的时候,没有人监管吗?作弊怎么办) 2.今天做的是比较基础的题,题型有判断题(8),选择题(10),简答题(3),编程(2)逻辑题(2) (ps:感觉做的还不 ...
- À peu près là 隐私政策
隐私政策 本应用尊重并保护所有使用服务用户的个人隐私权.为了给您提供更准确.更有个性化的服务,本应用会按照本隐私权政策的规定使用和披露您的个人信息.但本应用将以高度的勤勉.审慎义务对待这些信息.除本隐 ...
- Mysql常见问题集锦
缺少libstdc++.so.6库的原因及解决办法 https://blog.csdn.net/u010417185/article/details/69951312 https://www.cnbl ...
- VC添加全局热键的方法
VC添加全局热键的方法 这个方法靠谱 http://blog.csdn.net/lujianfeiccie2009/article/details/7498704 VC添加全局热键的方法 标签: bu ...
- C# AD 验证登陆
using System.DirectoryServices; using System.DirectoryServices.AccountManagement; using (DirectoryEn ...
- Oracle存储过程语法及编译过程讲解
语法 ? 1 2 3 4 5 6 7 8 9 10 CREATE [ORReplace]PROCEDURE[schema.] procedure_name [(argument [{IN|OUT|IN ...
- Redis中的LRU淘汰策略分析
Redis作为缓存使用时,一些场景下要考虑内存的空间消耗问题.Redis会删除过期键以释放空间,过期键的删除策略有两种: 惰性删除:每次从键空间中获取键时,都检查取得的键是否过期,如果过期的话,就删除 ...