-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
79 lines (71 loc) · 2.25 KB
/
Solution.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package ds.bfs.leetcode103;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* 二叉树的锯齿形层序遍历
* LeetCode 103 https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/
*
* @author yangyi 2021年04月30日18:53:44
*/
public class Solution {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
if (root == null) {
return new LinkedList<>();
}
List<List<Integer>> result = new LinkedList<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int level = 0;
while (!queue.isEmpty()) {
int size = queue.size();
LinkedList<Integer> levelList = new LinkedList<>();
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
if (cur != null) {
if (level % 2 == 0) {
levelList.addLast(cur.val);
} else {
levelList.addFirst(cur.val);
}
}
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
result.add(levelList);
level++;
}
return result;
}
public static void main(String[] args) {
TreeNode node_3 = new TreeNode(3);
TreeNode node_9 = new TreeNode(9);
TreeNode node_20 = new TreeNode(20);
TreeNode node_15 = new TreeNode(15);
TreeNode node_7 = new TreeNode(7);
node_3.left = node_9;
node_3.right = node_20;
node_20.left = node_15;
node_20.right = node_7;
Solution zigzagLevelOrder = new Solution();
List<List<Integer>> result = zigzagLevelOrder.zigzagLevelOrder(node_3);
for (List<Integer> integers : result) {
for (Integer integer : integers) {
System.out.print(integer + " ");
}
System.out.println();
}
}
}