From 56b3c9ba70ba0cd151405c3421c655c34900b842 Mon Sep 17 00:00:00 2001 From: Zhengtian CHU <40222298+lakerschampions@users.noreply.github.com> Date: Thu, 26 Aug 2021 16:29:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86559.n=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6=20JAVA?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E6=B3=95=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0104.二叉树的最大深度.md | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 7adb8bb7..cde3d460 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -311,6 +311,35 @@ class solution { } ``` +### 559.n叉树的最大深度 +```java +class solution { + /** + * 迭代法,使用层序遍历 + */ + public int maxDepth(Node root) { + if (root == null) return 0; + int depth = 0; + Queue que = new LinkedList<>(); + que.offer(root); + while (!que.isEmpty()) + { + depth ++; + int len = que.size(); + while (len > 0) + { + Node node = que.poll(); + for (int i = 0; i < node.children.size(); i++) + if (node.children.get(i) != null) + que.offer(node.children.get(i)); + len--; + } + } + return depth; + } +} +``` + ## python ### 104.二叉树的最大深度