Leetcode no. 111
2016-06-18 21:18
387 查看
111. 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.
/**
* 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;
return helper(root, 0);
}
private int helper(TreeNode root, int depth){
if (root.left== null && root.right== null) return depth+1;
int left=Integer.MAX_VALUE, right=Integer.MAX_VALUE;
if (root.left!= null) left= helper(root.left, depth+1);
if (root.right!= null) right= helper(root.right, depth+1);
return Math.min(left, right);
}
}
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;
return helper(root, 0);
}
private int helper(TreeNode root, int depth){
if (root.left== null && root.right== null) return depth+1;
int left=Integer.MAX_VALUE, right=Integer.MAX_VALUE;
if (root.left!= null) left= helper(root.left, depth+1);
if (root.right!= null) right= helper(root.right, depth+1);
return Math.min(left, right);
}
}
相关文章推荐
- SecureCRT简单的安装和激活教程
- python之数据类型(二)
- HQL:使用面向对象的HQL查询语言
- BeanUtil组件
- 指针数组和数组指针的区别
- 构建之法阅读笔记05
- [bzoj1797] [Ahoi2009]Mincut 最小割
- jQuery遍历
- Codeforces Round #358 (Div. 2)
- 附加作业
- 前序遍历和中序遍历重建二叉树
- 使用cocoaPods import导入时没有提示的解决办法
- GuavaCache简介(一)
- sql语句中的单双引号
- 附加题(一)——interesting的抄袭现象
- JVM: How to analyze Thread Dump
- Spring中的Jdbc事务管理
- Android中ListView的使用(三)----- 提升ListView运行效率
- 状压dp题目总结
- 20145223《Java程序程序设计》课程总结