LeetCode My Solution: Minimum Depth of Binary Tree
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的更多相关文章
- 【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 ...
- 【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 ...
- 【LeetCode OJ】Minimum Depth of Binary Tree
Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...
- 【一天一道LeetCode】#111. Minimum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 【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 ...
- 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 ...
- 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 ...
- LeetCode算法题-Minimum Depth of Binary Tree(Java实现)
这是悦乐书的第168次更新,第170篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第27题(顺位题号是111).给定二叉树,找到它的最小深度.最小深度是沿从根节点到最近的 ...
随机推荐
- javascript每日一练(二)——javascript(函数,数组)
一.函数 什么是函数 function show(){}//不带参数 function show(){return 123;}//不带参数,有返回值 function show(arg1, arg2, ...
- SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
问题的原因是无法找到org.slf4j.impl.StaticLoggerBinder,我找了一下,确实没有该类,网上搜了一下下面是官方的解答http://www.slf4j.org/codes.ht ...
- ios基础-编程规范
养成良好的编程习惯,是開始一门新语言的首要前提. (一)文档结构规范 1.建立Libs目录,存放第三方库 2.建立Tools目录,存放自己封装的类库 3.建立ViewControllers目录,存放全 ...
- C# - 数据库存取图片
1.创建数据表 CREATE TABLE Tb_pic ( ID int primary key identity(1, 1) not null, PictureBox varchar(max) ) ...
- AOP编程,spring实现及JDK,CGLIB实现
什么是AOP? AOP(Aspect-OrientedProgramming,面向方面编程)和OOP(Object-Oriented Programing,面向对象编程)思想不同,两者并非对立关系,前 ...
- 围观M$的new
围观M$的new 对于new一个类, M$为了拷贝和移动时的效率问题, 使用了非标准的new语法, 为了兼容性, 只能围观. http://blog.csdn.net/lostspeed/articl ...
- 基于visual Studio2013解决C语言竞赛题之1087数字变换
题目 解决代码及点评 /************************************************************************/ /* ...
- android 使用Scroller实现缓慢移动
在Launcher中的Workspace中实现了左右屏幕切换效果,里面就用到了Scroller记录滑动轨迹,实现一种缓慢地向左或向右移动的效果,这里我对这种效果进行总结: 我们先看一个例子:点击按钮时 ...
- Effective C++_笔记_条款07_为多态基类声明virtual析构函数
(整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 这个规则只适用于polymorphic(带多态性质的)base ...
- 使用JDK自带的工具将中文转换为ascii码
有时候在MyEclipse中,文件只能保存为“ISO-8859-1”的类型,而这种类型的文件时无法保存中文数据的,那么我们只能将中文数据经过Unicode编码才能往文件中保存,这里可以使用JDK自带的 ...