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. (三)初识jQuery

    进入jQuery官网:http://jquery.com/ 点击Download jQuery v3.1.1--->下载最新版本的jQuery版本--->放到你需要引入jQuery的文件中 ...

  2. VueJS事件处理器v-on

    事件监听可以使用 v-on 指令. v-on:click表达式 HTML: <!DOCTYPE html> <html> <head> <meta chars ...

  3. php图片本身有错无法显示的解决办法

    1.取消所有错误提示 2.如果没有报错,在header前(即设置输出格式前)使用ob_clean();

  4. spark之pycharm开发设置

    方法一: __author__ = 'similarface' import os import sys os.environ['SPARK_HOME']="/Users/similarfa ...

  5. java线程用法

    package com; public class Demo { public static void main(String[] args) { // TODO Auto-generated met ...

  6. 【WPF学习笔记】之如何点登录按钮时判断用户名密码进行登录:动画系列之(二)

    ...... 承接动画系列之(一)的代码: 再添加登录按钮代码进行登录,验证用户名和密码在数据库是否正确. 直接上代码: using System; using System.Collections. ...

  7. Swift实战(一): 剪子包袱锤ios应用

    来自十奶的大作业教学视频. http://www.swiftv.cn/course/ic2tqzob 主要了解了MVC模型. 首先是View,通过设计mainstoryboard构建UI界面,主要靠拖 ...

  8. 转载 ---原生和H5交互挺多的,最近也有朋友再问。这儿我写个简单的例子给大家 直接贴代码 js的

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> ...

  9. Windows系统下正确安装MongoDB

    1.下载.安装 官网下载: http://www.mongodb.org/downloads 下载好之后,接下来进行安装了: 2.创建数据文件夹 MongoDB将数据文件夹存储在 db 文件夹下. 可 ...

  10. C#定时检測子线程是否已经完毕

    C#定时检測子线程是否已经完毕 class Program { static void Main(string[] args) { //主线程中启动一个支线程,运行doSomething这种一个方法. ...