mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #650 from lakerschampions/master
更新了104. 二叉树的最大深度 JAVA版本
This commit is contained in:
@ -1528,6 +1528,29 @@ public:
|
||||
```
|
||||
|
||||
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:
|
||||
|
Reference in New Issue
Block a user