码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Minimun Depth of Binary Tree

时间:2014-09-12 01:08:32      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   div   sp   log   

 1 public class Solution {
 2     public int minDepth(TreeNode root) {
 3         if (root==null) return 0;
 4         List<TreeNode> upperLevelList = new ArrayList<TreeNode>();
 5         upperLevelList.add(root);
 6         int depth=1;
 7         while(true) {
 8             List<TreeNode> nowLevelList = new ArrayList<TreeNode>();
 9             for(int i=0; i<upperLevelList.size(); i++) {
10                 TreeNode tmpNode = upperLevelList.get(i);
11                 if(tmpNode.left==null && tmpNode.right==null) return depth;
12                 if (tmpNode.left!=null) nowLevelList.add(tmpNode.left);
13                 if (tmpNode.right!=null) nowLevelList.add(tmpNode.right);
14             }
15             upperLevelList = nowLevelList;
16             depth++;
17         }
18     }
19 }

 

[LeetCode] Minimun Depth of Binary Tree

标签:style   blog   color   io   ar   for   div   sp   log   

原文地址:http://www.cnblogs.com/yuhaos/p/3967480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!