Merge pull request #663 from lakerschampions/master

添加了559.n叉树的最大深度 JAVA迭代法版本
This commit is contained in:
程序员Carl
2021-08-27 09:04:42 +08:00
committed by GitHub

View File

@ -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<Node> 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 ## python
### 104.二叉树的最大深度 ### 104.二叉树的最大深度