题目描述

求给定二叉树的最小深度。最小深度是指树的根结点到最近叶子结点的最短路径上结点的数量。
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.
示例1

输入

复制

{1,2,3,4,5}

输出

复制

2

/**
 * struct TreeNode {
 *    int val;
 *    struct TreeNode *left;
 *    struct TreeNode *right;
 * };
 */

class Solution {
public:
    /**
     *
     * @param root TreeNode类
     * @return int整型
     */
    int run(TreeNode* root) {
        // write code here
        if (root==nullptr) return 0;
        if (root->left == nullptr)
        {
            return run(root->right) +1;
            
        }
        if (root->right == nullptr)
        {
            return run(root->left)+1;
            
        }
        int leftDepth=run(root->left);
        int rightDepth=run(root->right);
        return (leftDepth <rightDepth) ?(leftDepth+1):(rightDepth+1);
    }
    
};

/**
 * struct TreeNode {
 *    int val;
 *    struct TreeNode *left;
 *    struct TreeNode *right;
 * };
 */

class Solution {
public:
    typedef TreeNode *tree;
    /**
     *
     * @param root TreeNode类
     * @return int整型
     */
    int run(TreeNode* root) {
        // write code here
        if (!root) return 0;
        queue <tree> qu;
        tree last,now;
        int level,size;
        last=now=root;
        level=1;qu.push(root);
        while (qu.size()){
            now=qu.front();
            qu.pop();
            size=qu.size();
            if (now->left) qu.push(now->left);
            if (now->right) qu.push(now->right);
            if (qu.size()-size==0) break;
            if (last==now){
                level++;
                if (qu.size()) last=qu.back();
                
            }
        }
        return level;
        
    }
};

leetcode1Minimum Depth of Binary Tree的更多相关文章

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

  2. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

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

  6. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  7. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  8. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  9. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

随机推荐

  1. 活字格外联数据库SQLServer和Mysql的经验(大多数经验也适合其它使用外联数据库的平台)

    来自学习和实操后的总结,有说得不对的,或者遗漏的,大家留言补充.希望这个贴子,能成为活字格老铁们使用外联库的一个指南.PS即使你不打算使用外联库,里面的一些方法,也值得看一看! 一.库表规划1.系统表 ...

  2. ORA-00001: unique constraint (string.string) violated 违反唯一约束条件(.)

    ORA-00001: unique constraint (string.string) violated   ORA-00001: 违反唯一约束条件(.) Cause: An UPDATE or I ...

  3. vue的二级联动,数据是从php获取到的

    1.首先,一级要有change改变事件的关键字,v-on:change="selectarr($event)" 这是created(){}函数里面的自动调用一级分类的数据 html ...

  4. FastJson解析Json,封装JavaBean对象

    获取到前端的Json,后台对应封装JavaBean对象,对其解析赋值 获取到前端的json,对其进行分析 1.获取最外层前端json对应得JavaBean (1)未分析格式的json串 (2)初步格式 ...

  5. DE2资源集锦

    1.The School of Electrical and Computer Engineering (ECE) at the Georgia Institute of Technology:htt ...

  6. docker 升级后或者重装后,启动容器提示:Error response from daemon: Unknown runtime specified docker-runc

    之前安装的版本是docker 1.3,并运行了容器jenkins 现在把docker升级版本为docker-ce 19.03 再使用docker ps发现之前的jenkins容器已经退出了 启动容器: ...

  7. windows 快速安装Python3.7.2

    1.官方下载地址:https://www.python.org/downloads/release/python-372/ 其他地址:http://www.uzzf.com/soft/449550.h ...

  8. centos8平台使用ip命令代替ifconfig管理网络

    一,为什么建议使用ip命令代替ifconfig? 1,ifconfig所属的net-tools包已经不再被维护了 虽然可以用,但会发生看不到部分ip等情况, [root@centos8 liuhong ...

  9. CentOS安装MySQL详解 转

      引言 最近某云搞活动,买了个服务器作为平时学习和测试用,新机器啥也没有,一些常用软件的安装是免不了的,于是乎想着把安装过程都详细记录下来,一是做个备忘,二是给有需要的同学作个参考. Linux上安 ...

  10. css和js实现硬件加速渲染自定义滚动条

    听别人说用CSS的变换来实现渲染有硬件加速的效果,看到很多大网站都开始陆续使用上了,我也来说说怎么做,我这边实现的滚动条有自然滚动效果,看起来比较自然,说的再多不如直接写,让我们开始吧! 我们需要自己 ...