首页 > 代码库 > [LeetCode] Minimun Depth of Binary Tree

[LeetCode] Minimun Depth of Binary Tree

 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