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.二叉树的最大深度