Lintcode155-Minimum Depth of Binary Tree-Easy
155. Minimum Depth of Binary Tree
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.
Example
Example 1:
Input: {}
Output: 0
Example 2:
Input: {1,#,2,3}
Output: 3
Explanation:
1
\
2
/
3
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: The root of binary tree
* @return: An integer
*/
int minDepth = Integer.MAX_VALUE;
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
} else if (root.left == null && root.right != null) {
helper(root.right, 2);
} else if (root.right == null && root.left != null) {
helper(root.left, 2);
} else {
helper(root, 1);
}
return minDepth;
}
public void helper(TreeNode root, int curDepth) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
if (curDepth < minDepth) {
minDepth = curDepth;
}
return;
} helper(root.left, curDepth + 1);
helper(root.right, curDepth + 1);
}
}
public class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
return getMin(root);
}
public int getMin(TreeNode root){
if (root == null) {
return Integer.MAX_VALUE;
}
if (root.left == null && root.right == null) {
return 1;
}
return Math.min(getMin(root.left), getMin(root.right)) + 1;
}
}
Lintcode155-Minimum Depth of Binary Tree-Easy的更多相关文章
- 【leetcode】Minimum Depth of Binary Tree (easy)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- LeetCode_111. Minimum Depth of Binary Tree
111. Minimum Depth of Binary Tree Easy Given a binary tree, find its minimum depth. The minimum dept ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree
1. Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- 【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】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
随机推荐
- RedHat Enterprise Linux 6.4使用网易Centos 6 的yum源
1.首先到http://mirrors.163.com/centos下载软件包 x86 地址:http://mirrors.163.com/centos/6/os/i386/Packages/ x86 ...
- sql相同表不同查询条件合并显示
关键字:FULL JOIN 只要其中某个表存在匹配,FULL JOIN 关键字就会返回行. select a.createtime, ISNULL(lp, 0) lp , ISNULL(hp, 0) ...
- git reset与git revert的区别
http://alpha-blog.wanglianghome.org/2010/07/30/git-partial-rollback/ reset(版本撤回) 格式 git reset [-q] [ ...
- 如何查看端口recv和send
1.进入到pod的宿主机 一般来说 ssh slaveX 2.查看进程号 top可以看到 3.执行命令 nsenter --target 10594 --net netstat -an
- visual studio 中被遗忘的任务列表和书签
任务列表(Task List)是VS中被人遗忘的一个功能,用到跳转到不同的代码段非常不便.以后就不用每次前进和后退导航了. 使用“任务列表” 跟踪使用 TODO 和 HACK或自定义令牌等令牌的代码注 ...
- Mybatis框架(未完待续)
1.框架概述: 它是我们软件开发中的一套解决方案,不同的框架解决的是不同的问题.好处:框架封装了很多的细节,使开发者可以使用极简的方式实现功能.大大提高开 ...
- Backup &recovery备份和还原
实践版本:MySQL5.7 备份类型(backup type)物理和逻辑备份(Physical Versus Logical Backup) 物理备份是指直接复制存储数据库内容的目录和文 ...
- Qt QLabel QTextBrowser 实现网址链接
勾选属性: 并且编辑网址链接: QLabel--点击text属性的...: QTextBrowser--双击控件
- laravel----------laravel一些注意事项和一些说明
1.php artisan key:generate 解释:.env文件里面的APP_KEY参数设置为一个随机字符串也就是这个key是一个随机字符串,用于实现框架中的encrypt(加密)服务, ...
- C#6.0,C#7.0新特性
C#6.0新特性 Auto-Property enhancements(自动属性增强) Read-only auto-properties (真正的只读属性) Auto-Property Initia ...