From 0a348b8c0ac67e9eef048c498a2f9d2782663bb9 Mon Sep 17 00:00:00 2001 From: Zhengtian CHU <40222298+lakerschampions@users.noreply.github.com> Date: Wed, 25 Aug 2021 14:48:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86104.=20=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20JAVA=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index e62a3e66..a57a92aa 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1528,6 +1528,29 @@ public: ``` Java: +```Java +class Solution { + public int maxDepth(TreeNode root) { + if (root == null) return 0; + Queue que = new LinkedList<>(); + que.offer(root); + int depth = 0; + while (!que.isEmpty()) + { + int len = que.size(); + while (len > 0) + { + TreeNode node = que.poll(); + if (node.left != null) que.offer(node.left); + if (node.right != null) que.offer(node.right); + len--; + } + depth++; + } + return depth; + } +} +``` Python: