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

方法有:

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. java线程基础知识----线程基础知识

    不知道从什么时候开始,学习知识变成了一个短期记忆的过程,总是容易忘记自己当初学懂的知识(fuck!),不知道是自己没有经常使用还是当初理解的不够深入.今天准备再对java的线程进行一下系统的学习,希望 ...

  2. 浅谈.net的后台校验

    1.场景描述 在开发中,前端的相关模型校验往往不能满足当前开发的需求,也就是并不是十分的安全.于是,很多情况下需要后端进行模型的校验.在.net mvc中,有很多校验的方式(比如:值(1)可以使用内置 ...

  3. PM2使用文档

    简介 PM2是node进程管理工具,可以利用它来简化很多node应用管理的繁琐任务,如性能监控.自动重启.负载均衡等,而且使用非常简单. 下面就对PM2进行入门性的介绍,基本涵盖了PM2的常用的功能和 ...

  4. github上虽然已经有实现好的Char RNN

    前言 学习RNN的时候很多人应该都有看过Andrej Karpathy写的The Unreasonable Effectiveness of Recurrent Neural Networks,使用基 ...

  5. day20模块作业

    1.模块化作业 1.回顾文件递归遍历. 默写一遍. 入口在: 当文件是个文件夹的时候 出口在: 文件是一个文件 2.计算时间差(用户输入起始时间和结束时间. 计算时间差(小时), 例如, 用户输入20 ...

  6. .net core webapi框架

    折腾了一段时间..还是用ef比较熟,哈哈 参考地址:https://www.cnblogs.com/danvic712/p/10331976.html --jwt 参考地址:https://www.c ...

  7. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  8. spring boot war

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  9. ERROR: Unable to globalize '/usr/local/NONE/etc/php-fpm.d/*.conf' (ret = 2) from /usr/local/etc/php-fpm.conf at line WARNING: Nothing matches the include pattern '/usr/local/php7/etc/php-fpm.d/*.conf'

    Building from source is not easy if something is a bit different, and I had a hard time with some di ...

  10. @Inherited:允许子类继承父类的注解。

    在看定义注解的相关文章的时候,看到这个@Inherited注解,简单的说明并没有真正搞懂是什么意思.在网上搜索了一些相关的内容,现在把一篇文章转载过来.以便后面使用. 文章出处,转载地址:(http: ...