Minimum Depth of Binary Tree

Total Accepted: 24760 Total
Submissions: 83665My Submissions

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.

Have you been asked this question in an interview?

/**
* Definition for binary tree
* 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;
}
return helper(root);
}
//这个题目和求最大(最小深度)不一样的是要走到叶子节点才算行,也就说要到了叶子节点才OK
//最開始的时候採取了(root == null)的推断,报错,是由于对根节点的处理中觉得是求最大的深度。而最小的深度实际是到了
//叶子节点之后才算行
int helper(TreeNode root) {
if (root.left == null && root.right == null) {
return 1;
}
if (root.left == null) {
return helper(root.right) + 1;
}
if (root.right == null) {
return helper(root.left) + 1;
}
else {
return Math.min(helper(root.left),helper(root.right)) + 1;
}
}
}

LeetCode My Solution: Minimum Depth of Binary Tree的更多相关文章

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

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

  3. 【LeetCode OJ】Minimum Depth of Binary Tree

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

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

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

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

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

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

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

  9. LeetCode算法题-Minimum Depth of Binary Tree(Java实现)

    这是悦乐书的第168次更新,第170篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第27题(顺位题号是111).给定二叉树,找到它的最小深度.最小深度是沿从根节点到最近的 ...

随机推荐

  1. UVA 11292 - The Dragon of Loowater (water)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=sh ...

  2. 使用最新的log4cplus(1.1.1)隔离不同的 log 文件输出

    部分参考了博客. http://www.cppblog.com/tx7do/articles/11719.html 基于脚本配置来过滤log信息 除了通过程序实现对log环境的配置之外,log4cpl ...

  3. 读书与写论文的引导书——leo鉴书60

    我是专科直接考的研究生.在论文写作方面基本能够算是初级.MBA毕业那会儿要写论文,在网上找了不少这方面的书,<论文与治学>是当中之中的一个. 这本那时为应景儿卖的书,成了我之后学习与工作的 ...

  4. jQuery Easy UI Resizable(调整大小)组件

    Resizable(调整大小)组件,easyui基础组件之中的一个.调整大小就是能够对元素能够拖着调整大小,这个组件不依赖于其它组件,使用比較简单,相关的属性.事件都 在样例中介绍了. 演示样例: & ...

  5. poj 百练 2765 八进制小数(精度问题)

    2765:八进制小数 查看 提交 统计 提示 提问 总时间限制:  1000ms  内存限制:  65536kB 描写叙述 八进制小数能够用十进制小数精确的表示.比方,八进制里面的0.75等于十进制里 ...

  6. Endnote X6 如何修改输出格式(output style)成为自己想要的输出格式:

    Endnote X6 如何修改输出格式(output style)成为自己想要的输出格式: (1)首先尝试在endnote output style 网站中查找: http://www.endnote ...

  7. 关于for循环中i=0与i=arr.length容易被忽视的bug

    for循环中的这两种写法 for(var i=0,len=arr.length;i<len;i++){ } 上面这种是最为常见也是初学者经常写的 而下面这种写法,在性能上则是比上面的更好,然而我 ...

  8. PHP学习之-面向对象

    PHP学习之-面向对象 1.什么是对象 "世界万物皆对象",一切可以被抽象出来的东西都是对象.像花,草.看不到的"概念"都是对象. 2.对象的基本组成 a.属性 ...

  9. listbox多选实现上下移动 js版和服务器版

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200906021128.as ...

  10. Effective C++_笔记_条款06_若不想使用编译器自动生成的函数,就该明确拒绝

    (整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 通常如果你不希望class支持某一特定机能,只要不声明对应函数就 ...