题意:输出一个二叉树的最小深度。

思路:搜索一下就行了。

注意:搜索的时候,是比较每个子树的左右子树的大小,每个子树的深度要加上根节点!

class Solution {
public:
int run(TreeNode *root) {
if (root == NULL) return ; //空树
if (root->left == NULL) return run(root->right) + ;
if (root->right == NULL) return run(root->left) + ;
int left = run(root->left);
int right = run(root->right);
return (left < right) ? (left+) : (right+);
}
};

兄弟题

maximum-depth-of-binary-tree

题意:输出最大的二叉树的深度

class Solution {
public:
int maxDepth(TreeNode *root) {
if (root == NULL)return ;
if (root->left == NULL) maxDepth(root->right) + ;
if (root->right == NULL)maxDepth(root->left) + ;
int left = maxDepth(root->left) + ;
int right = maxDepth(root->right) + ;
return left > right ? left : right;
}
};

minimum-depth-of-binary-tree (搜索)的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

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

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

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

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

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

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

  7. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

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

  9. 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)

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

  10. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

随机推荐

  1. SpringBoot入门之基于注解的Mybatis

    今天学习下SpringBoot集成mybatis,集成mybatis一般有两种方式,一个是基于注解的一个是基于xml配置的.今天先了解下基于注解的mybatis集成. 一.引入依赖项 因为是mybat ...

  2. 牛刀小试MySQL--innodb_flush_log_at_trx_commit小结

    参数名:innodb_flush_log_at_trx_commit 参数值: 0 事务提交的时候,不会去刷日志缓存区,也不会立马写入至日志文件中.这种设置最危险.如果数据库挂了且运气不好,数据库的最 ...

  3. 打印小票,使用的是BarcodeLib

    打印 private void Control_Click(object s,EventArgs e) { if (((Control)s).Name == "button1") ...

  4. laravel使用JSON 类型方式进行存储

    Laravel 从 5.0 版本开始就已支持 JSON 数据的转换,但这样做的目的只是为了方便数据处理.你的数据依然以 TEXT 类型存放在你的数据库里.不过 MySQL 从 5.7 版本起开始支持原 ...

  5. Java基础——Oracle(八)

    一.流程控制语句 1) 循环语句 == loop ..  end loop 简单的循环,至少被执行一次 create table userinfo (id number, name varchar2( ...

  6. 在线客服兼容谷歌Chrome、苹果Safari、Opera浏览器的修改

    纵览全网提供的众多号称兼容多浏览器的自动收缩在线客服,其实只兼容了IE和FF两种,当遇到谷歌Chrome.苹果Safari.Opera浏览器时鼠标还没点到客服按钮就会自动缩回,实用效果完全打折 以下代 ...

  7. Linux 学习笔记之超详细基础linux命令 Part 11

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 10---------------- ...

  8. MIPS 安全相关paper阅读笔记

    前言 论文来自 ​ https://cyber-itl.org/2018/12/07/a-look-at-home-routers-and-linux-mips.html Linux_MIPS_mis ...

  9. 微信小程序开发--路由切换,页面重定向

    这段时间开发了一个微信小程序,虽然小程序的导航API 官方文档写得很详细,但是在具体开发过程中还是会遇到很多不明白,或者一时转不过弯的地方. 1.页面切换传参,参数读取 1.1  wx.navigat ...

  10. Video.js web视频播放器

    免费视频播放器videojs中文教程 Video.js是一款web视频播放器,支持html5和flash两种播放方式.更多关于video.js的介绍,可以访问官方网站介绍,我之前也写过一篇关于vide ...