From f07fb2fd195b54809474dd8e4997e2654f453f04 Mon Sep 17 00:00:00 2001 From: ColorQian <82574279+ColorQian@users.noreply.github.com> Date: Sat, 21 Aug 2021 17:21:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=20=20111.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6=20=E7=9A=84java?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index dde5af47..b9bc05e0 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.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: