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.
出处:http://www.programcreek.com/2013/02/leetcode-minimum-depth-of-binary-tree-java/
下边的这个解法简直是机智:维护两个List,其中count的那个List尤为重要,都跟节点同步,add或者poll元素,记录当前元素是第几层,简直是不能更机智。如果不懂了,画一个图走一下流程,就体会出来了
/**
* 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;
LinkedList<TreeNode> list=new LinkedList<TreeNode>();
LinkedList<Integer> count = new LinkedList<Integer>();
list.add(root);
count.add(1);
while(!list.isEmpty()){
TreeNode temp=list.poll();
int cal=count.poll();
if(temp.left==null && temp.right==null) return cal;
if(temp.left!=null){
list.add(temp.left);
count.add(cal+1); //这个和下边的实现了第n层有几个节点,就让这个n存储几个
}
if(temp.right!=null){
list.add(temp.right);
count.add(cal+1); //这个和上边的实现了第n层有几个节点,就让这个n存储几个
}
}
return count.poll(); //必须有返回语句,不然报错
}
}
下边的转自:http://blog.youkuaiyun.com/sbitswc/article/details/26526031
下边我故意交叉用了ArrayList和 LinkedList,而在对outter用Inner进行赋值的时候竟然没有出错,可见ArrayList和 LinkedList的关系
public class Solution {
public int minDepth(TreeNode root) {
ArrayList<TreeNode>outter=new ArrayList<TreeNode>();
if(root==null) return 0;
outter.add(root);
int count=1;
while(!outter.isEmpty()){
LinkedList<TreeNode>inner=new LinkedList<TreeNode>();
for(TreeNode temp:outter){
if(temp.left==null && temp.right==null) return count;
if(temp.left!=null) inner.add(temp.left);
if(temp.right!=null) inner.add(temp.right);
}
++count; //在整个遍历完outter中的元素之后再行加一
outter=new ArrayList<TreeNode>(inner); //把inner赋给了outter,即保证每一层都在同一次迭代里边
}
return count;
}
}
下边的参考这篇博客:http://www.blogjava.net/menglee/archive/2013/12/21/407856.html
递归写法
public class Solution {
public int minDepth(TreeNode root) {
if(root==null) return 0;
else if(root.left==null && root.right==null) return 1;
else{ //有了上边的else语句,这儿必然满足root.left和root.right里边至少有一个肯定不为空
//下边之所以有Integer.MAX_VALUE,是因为left和right至少有一个为空,那么深度肯定以不为空的那个计,所以把这个置为最大,以防干扰
int left=root.left==null? Integer.MAX_VALUE:minDepth(root.left);
int right=root.right==null? Integer.MAX_VALUE:minDepth(root.right);
return Math.min(left,right)+1;
}
}
}
跟上一种写法思想一样,只不过在处理left和right有且仅有一个为空的时候写法稍微有点不同,没有用Integer.MAX_VALUE
public class Solution {
public int minDepth(TreeNode root) {
if(root==null) return 0;
else if(root.left==null&&root.right==null) return 1;
else {
int leftDepth=minDepth(root.left);
int rightDepth=minDepth(root.right);
if(leftDepth==0) return rightDepth+1;
else if(rightDepth==0) return leftDepth+1;
else return Math.min(leftDepth, rightDepth)+1;
}
}
}
下边的写法就是上边两种写法,只不过看看更简单了,之所以上边写的没有这么简单,真的是因为对于递归理解的不够好
出处:http://blog.youkuaiyun.com/xudli/article/details/8492064
public class Solution {
public int minDepth(TreeNode root) {
if(root==null) return 0;
if(root.left==null) return minDepth(root.right)+1;
if(root.right==null) return minDepth(root.left)+1;
else return Math.min(minDepth(root.right),minDepth(root.left))+1;
}
}
下边的程序对二叉树进行BFS,由于是按层遍历的,因此如果在某一层发现了一个叶子节点,那么就找到了最小深度,此时返回当前深度即可
public class Solution {
public int minDepth(TreeNode root) {
if(root==null) return 0;
int depth=1;
int curlevel=1; //物理意义是当前层
int nextlevel=0; //物理意义是下一层
Queue<TreeNode> queue=new LinkedList<TreeNode>(); //切记切记,queue是由LinkedList实现的
queue.add(root);
while(!queue.isEmpty()){
TreeNode node=queue.poll();
--curlevel; //每poll一个元素,当前层计数即curlevel减1
if(node.left==null && node.right==null) return depth;
if(node.left!=null){
queue.add(node.left);
++nextlevel; //基于当前层,如果有add,那么下一层计数即nextlevel加1
}
if(node.right!=null){
queue.add(node.right);
++nextlevel; //基于当前层,如果有add,那么下一层计数即nextlevel加1
}
if(curlevel==0){ //如果当前层为0,即所有元素从左到右遍历完
if(nextlevel!=0)
++depth; //如果下一层还有节点,那么depth加1
curlevel=nextlevel; //把下一层个数赋值给当前层
nextlevel=0; //下层置0
}
}
return depth;
}
}