题目描述

求给定二叉树的最小深度。最小深度是指树的根结点到最近叶子结点的最短路径上结点的数量。
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. P 3396 哈希冲突 根号分治

    Link 据说这是一道论文题????.具体论文好像是 集训队论文<根号算法--不只是分块> 根号分治的裸题. 首先我们考虑暴力怎么打. 先预处理出每个模数的答案,之后再 O(1) 的回答, ...

  2. 使用SignalR和XSLT进行实时注释

    下载source code - 10.1 MB Introduction  众所周知,web请求(HTTP请求)是根据请求/响应机制工作的.通过这种方式,作为客户机的浏览器使用GET或POST向服务器 ...

  3. 用c语言实现linux cat

    话不多说,直接上代码: #include <stdio.h> void file_copy(FILE * file1,FILE * file2); int main(int argc,ch ...

  4. android中判断一个链接是否是有效的

    private boolean isValid(String urlString) { try { URL url = new URL(urlString); return URLUtil.isVal ...

  5. 2017-01-26--编译busybox总结

    错误一: ox@ubuntu:busybox-1.16.0$ make menuconfig Makefile:431: *** mixed implicit and normal rules: de ...

  6. 多测师讲解jmeter _接口请求_(003)高级讲师肖sir

    1.简单接口的请求 2. 3. 正则查看: 正则提取:在后置处理器中正则请求 设置:正则表达式 JSESSIONID提取器: Debug  sampler 总结:

  7. 【故障公告】博客系统升级到 .NET 5.0 引发的故障

    昨天晚上我们将博客系统从 .NET Core 3.1 升级到了 .NET 5.0 ,本来是一次很有信心的升级,但没有想到在今天下午访问高峰时竟然出现了故障,大量请求访问速度变慢或者因为下面的数据库连接 ...

  8. excel 无效引用 所引用的单元格不能位于256列

    无效引用 该文件版本所包含的公式中,所引用的单元格不能位于256列(列IW 或更远)或 65536 行以外的区域. 原因及解决方法: 1.版本问题,把两文件都另存为一致的版本,改为后缀为xlsx. 2 ...

  9. 测试-python相关

    python基础 一行代码实现1~100累加,不使用for循环 方式一: sum(range(1,101)) 方式二: 1 from functools import reduce 2 reduce( ...

  10. scp带密码拷贝文件

    应用场景:将B服务器的文件传输到A服务器.核心命令: sshpass -p 123456 scp ubuntu@192.168.52.1:/home/ubuntu/"TEST"'' ...