/** * Definition for binary tree * 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> queue = new LinkedList<TreeNode>(); LinkedList<TreeNode> childQueue = new LinkedList<TreeNode>(); queue.add(root); int depth = 1; while (!queue.isEmpty()){ TreeNode node = queue.remove(); if(node.left==null && node.right==null){ return depth; } if(node.left!=null){ childQueue.add(node.left); } if(node.right!=null){ childQueue.add(node.right); } if(queue.isEmpty() && !childQueue.isEmpty()){ depth++; queue.addAll(childQueue); childQueue.clear(); } } return depth; } }
相关推荐
java java_leetcode-111-minimum-depth-of-binary-tree
javascript js_leetcode题解之111-minimum-depth-of-binary-tree.js
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
java java_leetcode-maximum-depth-of-binary-tree
python python_leetcode题解之111_Minimum_Depth_of_Binary_Tree
js js_leetcode题解之104-maximum-depth-of-binary-tree.js
javascript js_leetcode题解之156-binary-tree-upside-down.js
javascript js_leetcode题解之145-binary-tree-postorder-traversal.js
javascript js_leetcode题解之144-binary-tree-preorder-traversal.js
javascript js_leetcode题解之124-binary-tree-maximum-path-sum.js
js js_leetcode题解之102-binary-tree-level-order-traversal.js
js js_leetcode题解之107-binary-tree-level-order-traversal-ii.js
js js_leetcode题解之103-binary-tree-zigzag-level-order-traversal.js
javascript js_leetcode题解之94-binary-tree-inorder-traversal.js
javascript js_leetcode题解之173-binary-search-tree-iterator.js
js js_leetcode题解之105-construct-binary-tree-from-preorder
java java_leetcode题解之Maximum Binary Tree.java
js js_leetcode题解之106-construct-binary-tree-from-inorder
javascript js_leetcode题解之114-flatten-binary-tree-to-linked-list.js