Skip to content

Commit 39b11b2

Browse files
authored
Merge pull request gzc426#376 from coolbhy/patch-7
Create 啦啦啦.md
2 parents a290023 + 8a71f17 commit 39b11b2

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

2018.12.07-leetcode104/啦啦啦.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
```java
2+
/**
3+
* Definition for a binary tree node.
4+
* public class TreeNode {
5+
* int val;
6+
* TreeNode left;
7+
* TreeNode right;
8+
* TreeNode(int x) { val = x; }
9+
* }
10+
*/
11+
class Solution {
12+
public int maxDepth(TreeNode root) {
13+
if(root==null)return 0;
14+
Queue<TreeNode> q = new LinkedList<>();
15+
q.add(root);
16+
int level = 0;
17+
int next=0;
18+
int last=1;
19+
while(!q.isEmpty()){
20+
TreeNode r = q.poll();
21+
last--;
22+
if(r.left!=null){
23+
q.add(r.left);
24+
next++;
25+
}
26+
if(r.right!=null){
27+
q.add(r.right);
28+
next++;
29+
}
30+
31+
if(last==0){
32+
level++;
33+
last=next;
34+
next=0;
35+
}
36+
}
37+
return level;
38+
}
39+
}
40+
```

0 commit comments

Comments
 (0)