diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index dde5af47b6..b9bc05e06c 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1597,6 +1597,43 @@ public: ``` Java: +```java +class Solution { + + public int minDepth(TreeNode root){ + if (root == null) { + return 0; + } + + Queue queue = new LinkedList<>(); + queue.offer(root); + int depth = 0; + + while (!queue.isEmpty()){ + + int size = queue.size(); + depth++; + + TreeNode cur = null; + for (int i = 0; i < size; i++) { + cur = queue.poll(); + + //如果当前节点的左右孩子都为空,直接返回最小深度 + if (cur.left == null && cur.right == null){ + return depth; + } + + if (cur.left != null) queue.offer(cur.left); + if (cur.right != null) queue.offer(cur.right); + } + + + } + + return depth; + } +} +``` Python 3: