Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ml):$104.maximum-depth-of-binary-tree.md #450

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion problems/104.maximum-depth-of-binary-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var maxDepth = function (root) {

## 代码

- 语言支持:JS,C++,Python
- 语言支持:JS,C++,Java,Python

JavaScript Code:

Expand Down Expand Up @@ -149,6 +149,55 @@ public:
};
```

Java Code:

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root == null)
{
return 0;
}
// 队列
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
int res = 0;
// 按层扩展
while(!queue.isEmpty())
{
// 拿出该层所有节点,并压入子节点
int size = queue.size();
while(size > 0)
{
TreeNode node = queue.poll();

if(node.left != null)
{
queue.offer(node.left);
}
if(node.right != null)
{
queue.offer(node.right);
}
size-=1;
}
// 统计层数
res +=1;
}
return res;
}
}
```

Python Code:

```python
Expand Down