File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode(int x) { val = x; }
8
+ * }
9
+ * /
10
+ class Solution {
11
+ public List<List<Integer >> zigzagLevelOrder(TreeNode root) {
12
+ List<List<Integer >> result = new ArrayList<>();
13
+ if (root == null) return result;
14
+ LinkedList<TreeNode > queue = new LinkedList<>();
15
+ queue.add(root);
16
+ TreeNode current;
17
+ List<Integer > tempilist;
18
+ int ilistlen;
19
+ boolean isleftbegin = true;
20
+ while (!queue.isEmpty()) {
21
+ List<Integer > intlist = new ArrayList<>();
22
+ int qlen = queue.size();
23
+ for (int i = 0; i < qlen; i++) {
24
+ current = queue.poll();
25
+ intlist.add(current.val);
26
+ if (current.left != null)
27
+ queue.add(current.left);
28
+ if (current.right != null)
29
+ queue.add(current.right);
30
+ }
31
+ if (!isleftbegin) {
32
+ tempilist = new ArrayList<>();
33
+ ilistlen = intlist.size();
34
+ for (int i=0;i<ilistlen;i++){
35
+ tempilist.add(intlist.get(ilistlen-1-i));
36
+ }
37
+ intlist = tempilist;
38
+ }
39
+ result.add(intlist);
40
+ isleftbegin = !isleftbegin;
41
+ }
42
+ return result;
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments