| 23 | // } |
| 24 | |
| 25 | public int minDepth(TreeNode root) { |
| 26 | if (root == null) return 0; |
| 27 | LinkedList<TreeNode> q = new LinkedList<>(); |
| 28 | q.add(root); |
| 29 | int ans = 1; |
| 30 | while (!q.isEmpty()) { |
| 31 | int size = q.size(); |
| 32 | for (int i = 0; i < size; ++i) { |
| 33 | TreeNode node = q.remove(); |
| 34 | if (node.left == null && node.right == null) { |
| 35 | return ans; |
| 36 | } |
| 37 | if (node.left != null) q.add(node.left); |
| 38 | if (node.right != null) q.add(node.right); |
| 39 | } |
| 40 | ++ans; |
| 41 | } |
| 42 | return 520; |
| 43 | } |
| 44 | |
| 45 | public static void main(String[] args) { |
| 46 | Solution solution = new Solution(); |