Skip to content

Commit f7fb3a4

Browse files
committed
20190129
1 parent 6fe551f commit f7fb3a4

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

code/lc103.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package code;
2+
/*
3+
* 103. Binary Tree Zigzag Level Order Traversal
4+
* 题意:蛇形层次遍历
5+
* 难度:Medium
6+
* 分类:Stack, Tree, Breadth-first Search
7+
* 思路:层次遍历,加了个flag每次是在list头添加或尾添加
8+
* Tips:Bingo!
9+
*/
10+
import java.util.ArrayDeque;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.Queue;
14+
15+
public class lc103 {
16+
17+
public class TreeNode {
18+
int val;
19+
TreeNode left;
20+
TreeNode right;
21+
TreeNode(int x) { val = x; }
22+
}
23+
24+
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
25+
List<List<Integer>> res = new ArrayList<>();
26+
if(root==null) return res;
27+
Queue<TreeNode> q = new ArrayDeque();
28+
q.add(root);
29+
boolean flag = true;
30+
while(!q.isEmpty()){
31+
int size = q.size();
32+
List<Integer> ls = new ArrayList<>();
33+
while(size>0){
34+
TreeNode temp = q.remove();
35+
if(flag) ls.add(temp.val);
36+
else ls.add(0,temp.val); //在头部添加
37+
if(temp.left!=null) q.add(temp.left);
38+
if(temp.right!=null) q.add(temp.right);
39+
size--;
40+
}
41+
res.add(ls);
42+
flag = !flag;
43+
}
44+
return res;
45+
}
46+
}

code/lc116.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package code;
2+
/*
3+
* 116. Populating Next Right Pointers in Each Node
4+
* 题意:设置二叉树的next指针,指向同层右侧节点
5+
* 难度:Medium
6+
* 分类:Tree, Depth-first Search
7+
* 思路:自己写的递归,中间有冗余。迭代方法和较优的递归会利用先前已经设置的next指针,做下一步操作
8+
* Tips:复习时多看一下,自己没想起来利用已经设置好的指针
9+
*/
10+
public class lc116 {
11+
public class TreeLinkNode {
12+
int val;
13+
TreeLinkNode left, right, next;
14+
TreeLinkNode(int x) { val = x; }
15+
}
16+
public void connect(TreeLinkNode root) {
17+
if(root==null) return;
18+
helper(root.left, root.right);
19+
}
20+
21+
public void helper(TreeLinkNode root1, TreeLinkNode root2){//较慢,也能过
22+
if( root1==null || root2==null ) return;
23+
root1.next = root2;
24+
helper(root1.left, root1.right);
25+
helper(root1.right, root2.left); //会重复设置
26+
helper(root2.left,root2.right);
27+
}
28+
29+
public void connect2(TreeLinkNode root) {
30+
while(root!=null){
31+
TreeLinkNode start = root;
32+
while(start!=null){
33+
if(start.left!=null){
34+
start.left.next = start.right; //设置下一层的next指针
35+
if(start.next!=null){
36+
start.right.next = start.next.left;
37+
}
38+
}
39+
start = start.next;
40+
}
41+
root = root.left;
42+
}
43+
}
44+
}

code/lc118.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package code;
2+
3+
import java.util.List;
4+
5+
public class lc118 {
6+
public List<List<Integer>> generate(int numRows) {
7+
while(numRows>0){
8+
9+
}
10+
}
11+
}

readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@ Language: Java
6969
| 084 [Java](./code/lc84.java)
7070
| 085 [Java](./code/lc85.java)
7171
| 088 [Java](./code/lc88.java)
72+
| 091 [Java](./code/lc91.java)
7273
| 094 [Java](./code/lc94.java)
7374
| 096 [Java](./code/lc96.java)
7475
| 098 [Java](./code/lc98.java)
7576
| 101 [Java](./code/lc101.java)
7677
| 102 [Java](./code/lc102.java)
7778
| 104 [Java](./code/lc104.java)
7879
| 105 [Java](./code/lc105.java)
80+
| 108 [Java](./code/lc108.java)
7981
| 114 [Java](./code/lc114.java)
8082
| 121 [Java](./code/lc121.java)
8183
| 122 [Java](./code/lc122.java)

0 commit comments

Comments
 (0)