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. Spring实战之装配Bean

    1.1Spring配置的可选方案 Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系.但是,作为开发人员,你需要告诉Spring要创建哪些bean并且如何将其装配在一起.当 ...

  2. leetcode746

    class Solution { public: int minCostClimbingStairs(vector<int>& cost) { vector<); totol ...

  3. 3DMAX 处理反面

    问题起源:从3DMAX导出一个模型为FBX后,在U3D中看到模型很奇怪的透视了,能看到背面看不到正面,这不法线问题,而是面反了. 即然是面反了,为什么在MAX中看起来是正确的呢? 应该是开启了双面模式 ...

  4. jar 运行报错:找不到或无法加载主类

    NIFEST.MF文件中指定的,如下所示:Manifest-Version: 1.0Class-Path: .Main-Class: com.webex.app.Main             // ...

  5. 如何建立ElasticSearch里的mappings?

    刚接触elasticsearch,好多东西都不会用,百度了很多,都看不懂,终于摸索出了最简单的通过http建立mappings的方法~ 有人在建立mappings报各种错误,首先,如果你的这个索引中已 ...

  6. SData:优雅的数据交换方案

    SData的网址是https://github.com/knat/SData. 数据交换方案可以分为两类:有纲要(schema)的和无纲要的.有纲要的数据交换方案有Google的Protocol Bu ...

  7. 使用JavaScript调用aspx后台代码

    方法1:js同步调用 触发: onclick="javascript:share('<%# Eval("id_File") %>')" 页面函数: ...

  8. pl/sql中if语句的使用

  9. vs code 配置spring boot开发环境

    一.环境变量 jdk环境变量一键设置 管理員运行 - 一支小白 - 博客园https://www.cnblogs.com/startnow/p/7416533.html 二.安装插件 1.Java E ...

  10. centos7 修改静态ip 和dns

    1.修改对应网卡的IP地址的配置文件# vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 #描述网卡对应的设备别名,例如ifcfg-et ...