-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution103.java
52 lines (43 loc) · 1.14 KB
/
Solution103.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package algorithm.leetcode;
import java.util.LinkedList;
import java.util.List;
/**
* @author: mayuan
* @desc: 二叉树的锯齿形层次遍历
* @date: 2019/02/27
*/
public class Solution103 {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> ans = new LinkedList<>();
if (null == root) {
return ans;
}
levelOrder(ans, root, 0);
return ans;
}
public void levelOrder(List<List<Integer>> answer, TreeNode node, int depth) {
if (null == node) {
return;
}
// 遍历到新的一层
if (answer.size() <= depth) {
answer.add(new LinkedList<>());
}
// 奇数层倒序(头插法)
if (1 == (depth & 1)) {
answer.get(depth).add(0, node.val);
} else {
answer.get(depth).add(node.val);
}
levelOrder(answer, node.left, depth + 1);
levelOrder(answer, node.right, depth + 1);
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}