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.

题意很清楚,找到二叉树中深度最短的一条路径,DFS(貌似是面试宝典上的一道题)

类似的一道题:http://www.cnblogs.com/double-win/p/3737262.html

 /**
* 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==NULL) return ;
if(root->left==NULL && root->right==NULL) return ; int Leftdepth = minDepth(root->left);
int Rightdepth = minDepth(root->right); if(Leftdepth==)
return Rightdepth+;
else if(Rightdepth==)
return Leftdepth+; return min(Leftdepth,Rightdepth)+;
}
};

[LeetCode 题解]: 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 OJ Minimum Depth of Binary Tree 递归求解

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

  4. [LeetCode] 111. 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][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 ...

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

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

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

  8. LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)

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

  9. leetcode 111 Minimum Depth of Binary Tree(DFS)

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

随机推荐

  1. C语言调用汇编

    程序的入口是main,在main里调用汇编的函数. 首先要解决怎么定义函数的问题 在C语言中,要extern 一个函数声明即可,然后这个函数在汇编里面实现. 在汇编里面,用EXPORT 把C语言定义的 ...

  2. 在kindeditor 获取textarea 中 输入的值

    要在kindeditor 获取textarea 中 输入的值 必须在kindeditor创建的时候添加下面红色字体的代码     kindeditor创建代码如下: var editor;KindEd ...

  3. spring-boot-actuator健康监控

    #健康监控 management.security.enabled=false health.mail.enabled =false http://localhost:54001/autoconfig ...

  4. Eclipse注释配置

    新的文件/** * @ClassName: ${type_name}  * @Description: ${todo} * @author ${user} * @date ${date} ${time ...

  5. redis 开发与运维 学习心得1

    主要是命令相关 第一章 初识Redis 1.redis是基于键值对的NoSQL. 2.redis的值可以是 string, hash, list, set, zset, bitmaps, hyperl ...

  6. centos7 yum 安装jq

    一.简介 EPEL是企业版 Linux 附加软件包的简称,EPEL是一个由Fedora特别兴趣小组创建.维护并管理的,针对 红帽企业版 linux(RHEL)及其衍生发行版(比如 CentOS.Sci ...

  7. java 修改文件

    public void fileAlter(String fileName,String content) throws IOException{                 BufferedRe ...

  8. redis入门资源收集汇总

    redis安装:http://www.redis.io/download redis命令测试平台:http://try.redis.io/ redis桌面管理工具:http://redisdeskto ...

  9. windows下python安装Numpy和Scipy模块

    安装 numpy: 去 http://sourceforge.net/projects/numpy/files/latest/download?source=files 下载相应的exe安装文件. 安 ...

  10. 398. Random Pick Index随机pick函数

    [抄题]: Given an array of integers with possible duplicates, randomly output the index of a given targ ...