-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathLeetCode_103_2.java
More file actions
152 lines (140 loc) · 4.45 KB
/
LeetCode_103_2.java
File metadata and controls
152 lines (140 loc) · 4.45 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//Given a binary tree, return the zigzag level order traversal of its nodes' values.
// (ie, from left to right, then right to left for the next level and alternate between).
//
//
//For example:
//Given binary tree [3,9,20,null,null,15,7],
//
// 3
// / \
// 9 20
// / \
// 15 7
//
//
//
//return its zigzag level order traversal as:
//
//[
// [3],
// [20,9],
// [15,7]
//]
//
//
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
package com.llz.algorithm.algorithm2019.secondweek;
import java.util.*;
import com.llz.algorithm.algorithm2019.firstweek.TreeNode;
public class LeetCode_103_2 {
/**
* Original version
* Time complexity is O(n), space complexity is O(n)
*
* @param root
* @return
*/
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if (root == null) return list;
Deque<TreeNode> queue = new ArrayDeque<>();
Deque<TreeNode> innerQueue = new ArrayDeque<>();
queue.add(root);
List<Integer> innerList = new ArrayList<>();
innerList.add(root.val);
list.add(innerList);
int size = 0;
boolean leftToRight = false;
TreeNode temp = null;
while (!queue.isEmpty()) {
size = queue.size();
innerList = new ArrayList<>();
for (int i = 0; i < size; i++) {
temp = queue.removeLast();
if (leftToRight) {
if (temp.left != null) {
innerQueue.add(temp.left);
innerList.add(temp.left.val);
}
if (temp.right != null) {
innerQueue.add(temp.right);
innerList.add(temp.right.val);
}
} else {
if (temp.right != null) {
innerQueue.add(temp.right);
innerList.add(temp.right.val);
}
if (temp.left != null) {
innerQueue.add(temp.left);
innerList.add(temp.left.val);
}
}
}
if (innerList.size() != 0)
list.add(innerList);
queue = innerQueue;
innerQueue = new ArrayDeque<>();
leftToRight = leftToRight == true ? false : true;
}
return list;
}
/**
* A more cleaner and easier code referenced from discussion
* with only one queue compared with two queues that used by my original version.
* Remember better utilize JDK API, think about using list.add(index, val) instead of construct an stack.
* Time complexity is O(n) and space complexity is O(n) as well.
*
* @param root
* @return
*/
public List<List<Integer>> zigzagLevelOrder2(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
boolean leftToRight = false;
Queue<TreeNode> queue = new ArrayDeque<>();
List<Integer> list = new ArrayList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode temp = queue.remove();
if (leftToRight)
list.add(0, temp.val);
else
list.add(temp.val);
if (temp.left != null)
queue.add(temp.left);
if (temp.right != null)
queue.add(temp.right);
}
leftToRight = !leftToRight;
res.add(list);
list = new ArrayList<>();
}
return res;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(7);
root.left.left.left = new TreeNode(8);
root.left.right.left = new TreeNode(9);
root.right.right.left = new TreeNode(10);
LeetCode_103_2 lc = new LeetCode_103_2();
List<List<Integer>> list = lc.zigzagLevelOrder2(root);
System.out.println(list);
}
}