更新了104. 二叉树的最大深度 JAVA版本

This commit is contained in:
Zhengtian CHU
2021-08-25 14:48:52 +08:00
committed by GitHub
parent 7e60e1946b
commit 0a348b8c0a

View File

@ -1528,6 +1528,29 @@ public:
``` ```
Java Java
```Java
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> 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 Python