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.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
if(root == null) return 0; int leftmin = minDepth(root.left);
int rightmin = minDepth(root.right); if(leftmin==0) return rightmin + 1;
if(rightmin==0) return leftmin + 1;
return leftmin>rightmin?rightmin+1:leftmin+1;
}
}

LeetCode OJ 111. Minimum Depth of Binary Tree的更多相关文章

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

  2. 【LeetCode OJ】Minimum Depth of Binary Tree

    Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...

  3. 【一天一道LeetCode】#111. Minimum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

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

  6. LeetCode OJ: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 ☆(二叉树的最小深度)

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

  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 My Solution: Minimum Depth of Binary Tree

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

随机推荐

  1. sqlserver跨数据库与跨服务器使用

    sqlserver跨数据库与跨服务器使用 日常数据库编程中经常会遇到需要跨数据库和跨服务器的情况,本文总结解决方法,具体如下: 工具/原料 SQLSERVER 数据库 方法/步骤   跨数据库使用比较 ...

  2. 郑州尚学堂:如何看待ARM的各种模式?

    嵌入式设备已经越来越与我们的日常生活密切相关了,由此带来了ARM的高速发展.就拿我们的手机来说吧,几乎所有的手机都是ARM体系的.这里大致介绍下ARM 的7种执行模式. ARMv4以上版本的CPU任何 ...

  3. java之String类型

    一:定义 String是复杂类型,是特殊的复杂类型. 二:创建 两种创建形式: String s = "abc"; String s = new String("abc& ...

  4. DHCP底层参考

    [原创翻译,水平有限] ISC DHCP支持802.1的以太网帧,令牌环和FDDI等网络.为了桥接物理层和DHCP层,它还必须实现IP和UDP协议帧. 这源于UNIX BSD socket 对未配置接 ...

  5. hdu1002

    //c//https://github.com/ssdutyuyang199401/hduacm/blob/master/1002.c#include<stdio.h>#include&l ...

  6. Hadoop查看目录文件大小的脚本

    hadoop fs -du / | awk '{ sum=$1 ;dir2=$3 ; hum[1024**3]="Gb";hum[1024**2]="Mb";h ...

  7. ViewBag的简单使用

    一,在控制器中写好数据绑定 //通过ID查找出整列的数据            Case.Models.Case theCase = db.Case.Find(id);            View ...

  8. background-clip与background-origin两者的区别

    第一篇随笔有提到 background-clip与background-origin两者的区别,但是太字面化了,对于小白而言甚是难以理解,包括我自己,在第二次去理解的时候,反而蒙圈了.所以,查阅了一些 ...

  9. ckplayer 参数设置详解

    参数   使用说明 f s=0时地为普通的视频地址s=1时是一个网址,网址里存放视频地址s=2时是一个网址,网址里输出xml格式的视频地址s=3时是一个swf文件地址,swf和播放器进行交互读取地址 ...

  10. POJ 3537 Crosses and Crosses(SG/还未想完全通的一道SG)

    题目链接 #include<iostream> #include<cstdio> #include<cstring> using namespace std; ]; ...