Skip to content

Commit 616ea7b

Browse files
authored
feat: add solutions to lc problem: No.0103 (doocs#2345)
No.0103.Binary Tree Zigzag Level Order Traversal
1 parent d98ba9f commit 616ea7b

File tree

6 files changed

+177
-168
lines changed

6 files changed

+177
-168
lines changed

solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/README.md

+59-56
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ class Solution {
143143
public:
144144
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
145145
vector<vector<int>> ans;
146-
if (!root) return ans;
146+
if (!root) {
147+
return ans;
148+
}
147149
queue<TreeNode*> q{{root}};
148150
int left = 1;
149151
while (!q.empty()) {
@@ -152,10 +154,16 @@ public:
152154
auto node = q.front();
153155
q.pop();
154156
t.emplace_back(node->val);
155-
if (node->left) q.push(node->left);
156-
if (node->right) q.push(node->right);
157+
if (node->left) {
158+
q.push(node->left);
159+
}
160+
if (node->right) {
161+
q.push(node->right);
162+
}
163+
}
164+
if (!left) {
165+
reverse(t.begin(), t.end());
157166
}
158-
if (!left) reverse(t.begin(), t.end());
159167
ans.emplace_back(t);
160168
left ^= 1;
161169
}
@@ -220,23 +228,25 @@ func zigzagLevelOrder(root *TreeNode) (ans [][]int) {
220228
*/
221229

222230
function zigzagLevelOrder(root: TreeNode | null): number[][] {
223-
const res = [];
224-
if (root == null) {
225-
return res;
231+
const ans: number[][] = [];
232+
if (!root) {
233+
return ans;
226234
}
227-
let isDesc = false;
228-
const queue = [root];
229-
while (queue.length !== 0) {
230-
const arr = queue.slice().map(() => {
231-
const { val, left, right } = queue.shift();
232-
left && queue.push(left);
233-
right && queue.push(right);
234-
return val;
235-
});
236-
res.push(isDesc ? arr.reverse() : arr);
237-
isDesc = !isDesc;
235+
const q: TreeNode[] = [root];
236+
let left: number = 1;
237+
while (q.length) {
238+
const t: number[] = [];
239+
const qq: TreeNode[] = [];
240+
for (const { val, left, right } of q) {
241+
t.push(val);
242+
left && qq.push(left);
243+
right && qq.push(right);
244+
}
245+
ans.push(left ? t : t.reverse());
246+
q.splice(0, q.length, ...qq);
247+
left ^= 1;
238248
}
239-
return res;
249+
return ans;
240250
}
241251
```
242252

@@ -264,34 +274,33 @@ use std::cell::RefCell;
264274
use std::collections::VecDeque;
265275
impl Solution {
266276
pub fn zigzag_level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
267-
let mut res = vec![];
268-
if root.is_none() {
269-
return res;
270-
}
271-
let mut is_desc = false;
272-
let mut q = VecDeque::new();
273-
q.push_back(root);
274-
while !q.is_empty() {
275-
let mut arr = vec![];
276-
for _ in 0..q.len() {
277-
if let Some(node) = q.pop_front().unwrap() {
278-
let mut node = node.borrow_mut();
279-
arr.push(node.val);
280-
if node.left.is_some() {
281-
q.push_back(node.left.take());
282-
}
283-
if node.right.is_some() {
284-
q.push_back(node.right.take());
277+
let mut ans = Vec::new();
278+
let mut left = true;
279+
if let Some(root_node) = root {
280+
let mut q = VecDeque::new();
281+
q.push_back(root_node);
282+
while !q.is_empty() {
283+
let mut t = Vec::new();
284+
for _ in 0..q.len() {
285+
if let Some(node) = q.pop_front() {
286+
let node_ref = node.borrow();
287+
t.push(node_ref.val);
288+
if let Some(ref left) = node_ref.left {
289+
q.push_back(Rc::clone(left));
290+
}
291+
if let Some(ref right) = node_ref.right {
292+
q.push_back(Rc::clone(right));
293+
}
285294
}
286295
}
296+
if !left {
297+
t.reverse();
298+
}
299+
ans.push(t);
300+
left = !left;
287301
}
288-
if is_desc {
289-
arr.reverse();
290-
}
291-
is_desc = !is_desc;
292-
res.push(arr);
293302
}
294-
res
303+
ans
295304
}
296305
}
297306
```
@@ -318,20 +327,14 @@ var zigzagLevelOrder = function (root) {
318327
let left = 1;
319328
while (q.length) {
320329
const t = [];
321-
for (let n = q.length; n; --n) {
322-
const node = q.shift();
323-
t.push(node.val);
324-
if (node.left) {
325-
q.push(node.left);
326-
}
327-
if (node.right) {
328-
q.push(node.right);
329-
}
330-
}
331-
if (!left) {
332-
t.reverse();
330+
const qq = [];
331+
for (const { val, left, right } of q) {
332+
t.push(val);
333+
left && qq.push(left);
334+
right && qq.push(right);
333335
}
334-
ans.push(t);
336+
ans.push(left ? t : t.reverse());
337+
q.splice(0, q.length, ...qq);
335338
left ^= 1;
336339
}
337340
return ans;

solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/README_EN.md

+59-56
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ class Solution {
139139
public:
140140
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
141141
vector<vector<int>> ans;
142-
if (!root) return ans;
142+
if (!root) {
143+
return ans;
144+
}
143145
queue<TreeNode*> q{{root}};
144146
int left = 1;
145147
while (!q.empty()) {
@@ -148,10 +150,16 @@ public:
148150
auto node = q.front();
149151
q.pop();
150152
t.emplace_back(node->val);
151-
if (node->left) q.push(node->left);
152-
if (node->right) q.push(node->right);
153+
if (node->left) {
154+
q.push(node->left);
155+
}
156+
if (node->right) {
157+
q.push(node->right);
158+
}
159+
}
160+
if (!left) {
161+
reverse(t.begin(), t.end());
153162
}
154-
if (!left) reverse(t.begin(), t.end());
155163
ans.emplace_back(t);
156164
left ^= 1;
157165
}
@@ -216,23 +224,25 @@ func zigzagLevelOrder(root *TreeNode) (ans [][]int) {
216224
*/
217225

218226
function zigzagLevelOrder(root: TreeNode | null): number[][] {
219-
const res = [];
220-
if (root == null) {
221-
return res;
227+
const ans: number[][] = [];
228+
if (!root) {
229+
return ans;
222230
}
223-
let isDesc = false;
224-
const queue = [root];
225-
while (queue.length !== 0) {
226-
const arr = queue.slice().map(() => {
227-
const { val, left, right } = queue.shift();
228-
left && queue.push(left);
229-
right && queue.push(right);
230-
return val;
231-
});
232-
res.push(isDesc ? arr.reverse() : arr);
233-
isDesc = !isDesc;
231+
const q: TreeNode[] = [root];
232+
let left: number = 1;
233+
while (q.length) {
234+
const t: number[] = [];
235+
const qq: TreeNode[] = [];
236+
for (const { val, left, right } of q) {
237+
t.push(val);
238+
left && qq.push(left);
239+
right && qq.push(right);
240+
}
241+
ans.push(left ? t : t.reverse());
242+
q.splice(0, q.length, ...qq);
243+
left ^= 1;
234244
}
235-
return res;
245+
return ans;
236246
}
237247
```
238248

@@ -260,34 +270,33 @@ use std::cell::RefCell;
260270
use std::collections::VecDeque;
261271
impl Solution {
262272
pub fn zigzag_level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
263-
let mut res = vec![];
264-
if root.is_none() {
265-
return res;
266-
}
267-
let mut is_desc = false;
268-
let mut q = VecDeque::new();
269-
q.push_back(root);
270-
while !q.is_empty() {
271-
let mut arr = vec![];
272-
for _ in 0..q.len() {
273-
if let Some(node) = q.pop_front().unwrap() {
274-
let mut node = node.borrow_mut();
275-
arr.push(node.val);
276-
if node.left.is_some() {
277-
q.push_back(node.left.take());
278-
}
279-
if node.right.is_some() {
280-
q.push_back(node.right.take());
273+
let mut ans = Vec::new();
274+
let mut left = true;
275+
if let Some(root_node) = root {
276+
let mut q = VecDeque::new();
277+
q.push_back(root_node);
278+
while !q.is_empty() {
279+
let mut t = Vec::new();
280+
for _ in 0..q.len() {
281+
if let Some(node) = q.pop_front() {
282+
let node_ref = node.borrow();
283+
t.push(node_ref.val);
284+
if let Some(ref left) = node_ref.left {
285+
q.push_back(Rc::clone(left));
286+
}
287+
if let Some(ref right) = node_ref.right {
288+
q.push_back(Rc::clone(right));
289+
}
281290
}
282291
}
292+
if !left {
293+
t.reverse();
294+
}
295+
ans.push(t);
296+
left = !left;
283297
}
284-
if is_desc {
285-
arr.reverse();
286-
}
287-
is_desc = !is_desc;
288-
res.push(arr);
289298
}
290-
res
299+
ans
291300
}
292301
}
293302
```
@@ -314,20 +323,14 @@ var zigzagLevelOrder = function (root) {
314323
let left = 1;
315324
while (q.length) {
316325
const t = [];
317-
for (let n = q.length; n; --n) {
318-
const node = q.shift();
319-
t.push(node.val);
320-
if (node.left) {
321-
q.push(node.left);
322-
}
323-
if (node.right) {
324-
q.push(node.right);
325-
}
326-
}
327-
if (!left) {
328-
t.reverse();
326+
const qq = [];
327+
for (const { val, left, right } of q) {
328+
t.push(val);
329+
left && qq.push(left);
330+
right && qq.push(right);
329331
}
330-
ans.push(t);
332+
ans.push(left ? t : t.reverse());
333+
q.splice(0, q.length, ...qq);
331334
left ^= 1;
332335
}
333336
return ans;

solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/Solution.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class Solution {
1313
public:
1414
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
1515
vector<vector<int>> ans;
16-
if (!root) return ans;
16+
if (!root) {
17+
return ans;
18+
}
1719
queue<TreeNode*> q{{root}};
1820
int left = 1;
1921
while (!q.empty()) {
@@ -22,10 +24,16 @@ class Solution {
2224
auto node = q.front();
2325
q.pop();
2426
t.emplace_back(node->val);
25-
if (node->left) q.push(node->left);
26-
if (node->right) q.push(node->right);
27+
if (node->left) {
28+
q.push(node->left);
29+
}
30+
if (node->right) {
31+
q.push(node->right);
32+
}
33+
}
34+
if (!left) {
35+
reverse(t.begin(), t.end());
2736
}
28-
if (!left) reverse(t.begin(), t.end());
2937
ans.emplace_back(t);
3038
left ^= 1;
3139
}

solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/Solution.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,14 @@ var zigzagLevelOrder = function (root) {
1919
let left = 1;
2020
while (q.length) {
2121
const t = [];
22-
for (let n = q.length; n; --n) {
23-
const node = q.shift();
24-
t.push(node.val);
25-
if (node.left) {
26-
q.push(node.left);
27-
}
28-
if (node.right) {
29-
q.push(node.right);
30-
}
22+
const qq = [];
23+
for (const { val, left, right } of q) {
24+
t.push(val);
25+
left && qq.push(left);
26+
right && qq.push(right);
3127
}
32-
if (!left) {
33-
t.reverse();
34-
}
35-
ans.push(t);
28+
ans.push(left ? t : t.reverse());
29+
q.splice(0, q.length, ...qq);
3630
left ^= 1;
3731
}
3832
return ans;

0 commit comments

Comments
 (0)