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.

题解:注意没有的分支不算。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int dfs(TreeNode *r){
if(r==NULL) return ;
if(r->left==NULL&&r->right==NULL){
return ;
}
else{
if(r->left==NULL){
return dfs(r->right)+;
}
else if(r->right==NULL){
return dfs(r->left)+;
}
else{
return min(dfs(r->left),dfs(r->right)) + ;
}
}
} int minDepth(TreeNode* root) {
return dfs(root);
}
};

leetcode 111 Minimum Depth of Binary Tree(DFS)的更多相关文章

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

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

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

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

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

  4. (二叉树 BFS DFS) 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 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

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

  7. leetcode 111 Minimum Depth of Binary Tree ----- java

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

  8. Java [Leetcode 111]Minimum Depth of Binary Tree

    题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

  9. Java for 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 ...

随机推荐

  1. 实用国际(XX)计量单位表

    很多实用附录简表:http://www.zdic.net/appendix/f1.htm 计量单位简表 时间的单位换算 : 1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)  1秒=1,00 ...

  2. awakeFromNib方法和viewDidLoad方法区别

    当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的awakeFromNib函数来响应这个消息,执行一些必要的操作. 也就是说只有通 ...

  3. iphone手机分辨率--持久维护

    6.5英寸 —— 1242 x 2688 px —— Xs Max 6.1英寸 —— 828 x 1792 px —— XR 5.8英寸 —— 1125 x 2436 px —— X/Xs 5.5英寸 ...

  4. (一)关于jQuery的网上资源

    jQuery官网: http://jquery.com/ jQuery API: http://jquery.cuishifeng.cn/ w3school学习网站:http://www.w3scho ...

  5. IE67实现inline-block布局

    inline-block可以定义元素为行内块级元素,即既具有行内元素同占一行的特点,又具有块级元素的box模型.但是IE67和其他浏览器的支持差别比较大: 1.行内元素使用inline-block变成 ...

  6. python之脚本参数optparse

    import optparse usage = "myprog[ -f <filename>][-s <xyz>] arg1[,arg2..]" opter ...

  7. Django之通过tag推荐文章

    #路由 views.py def post_detail(request,year,month,day,post): ''' 文章详情 + 评论详情 :param request: :param ye ...

  8. 【Atheros】如何在驱动中禁用ACK

    上一篇文章讲了如何禁用载波侦听(CSMA)和退避(BACKOFF)的方法,这一篇介绍如何禁用ACK. 禁用ACK主要分为三部分: 1. 在发送端设置不等待ACK回来就继续发送: 2. 在接收端设置收到 ...

  9. vs中使用M_PI的问题及解决 角度转弧度&根据弧度计算圆周上点的坐标的方法

    M_PI 是一个宏定义,圆周率的定义           C/C++ code #define M_PI 3.14159265358979323846 此宏定义和编译器有关,TC中M_PI宏就定义在& ...

  10. HDU 5343 MZL's Circle Zhou 后缀自动机+DP

    MZL's Circle Zhou Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...