题意:找到离根结点最近的叶子结点的那一层(设同一层上的结点与根结点的距离相等),返回它所在的层数。

方法有:

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 找最小深度(返回最小深度)的更多相关文章

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

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

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

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

  5. LeetCode - Minimum Depth of Binary Tree

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

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

  7. leetcode Minimum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  8. Minimum Depth of Binary Tree,求树的最小深度

    算法分析:递归和非递归两种方法. public class MinimumDepthofBinaryTree { //递归,树的最小深度,就是它左右子树的最小深度的最小值+1 public int m ...

  9. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

随机推荐

  1. hdu1062

    #include<stdio.h> #include<string.h> int main() { int i,n,len,j,k,t; char s1]; scanf(&qu ...

  2. 【Java面试题系列】:Java中final finally finalize的区别

    本篇为[Java面试题系列]第三篇,文中如有错误,欢迎指正. 第一篇链接:[Java面试题系列]:Java基础知识常见面试题汇总 第一篇 第二篇链接:[Java面试题系列]:Java基础知识常见面试题 ...

  3. jQuery之ajax() 参数

  4. Go:一个可能导致锁失效的坑

    先看代码: package main import( "sync" ) var hclock sync.RWMutex func main() { a := make(map[in ...

  5. SnapKit swift实现高度自适应的新浪微博布局

    SnapKit swift版的自动布局框架,第一次使用感觉还不错. SnapKit是一个优秀的第三方自适应布局库,它可以让iOS.OS X应用更简单地实现自动布局(Auto Layout).GtiHu ...

  6. 解决Nginx启动失败

    一.Nginx下载http://nginx.org/en/download.html 二.Nginx启动失败原因1.本人下载的是nginx-1.12.1(稳定版),下载完解压后,进入路径中,start ...

  7. Luogu P2455 [SDOI2006]线性方程组 真•高斯消元板子

    果然如Miracle学长所说...调了一天...qwq..还是过不了线下的Hack upd after 40min:刚刚过了 就是多了一个判无解的操作... 当系数都为0,且常数项不为0时,即为无解. ...

  8. 判断文件是否存在 local/hdfs

    在Linux文件系统中,我们可以使用下面的Shell脚本判断某个文件是否存在: # 这里的-f参数判断$file是否存在 if [ ! -f "$file" ]; then ech ...

  9. 练习十六:Python日期格式应用(datetime)

    练习:关于python日期格式应用练习.用python方法如何输出指定格式形式的日期 这里用到datetime模块,datetime模块重新封装了time模块,提供了更多接口,提供的类包括:date, ...

  10. aop 切面配置

    <bean id="userServiceImpl" class="com.bj.aop.xml.before.UserServiceImpl">& ...