mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #663 from lakerschampions/master
添加了559.n叉树的最大深度 JAVA迭代法版本
This commit is contained in:
@ -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.二叉树的最大深度
|
||||||
|
Reference in New Issue
Block a user