Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d40931e

Browse files
author
guangsheng.li01
committed
Solved 0103
1 parent efedb60 commit d40931e

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* [0103] binary-tree-zigzag-level-order-traversal
3+
*/
4+
5+
pub struct Solution {}
6+
use super::utils::tree::TreeNode;
7+
8+
// solution impl starts here
9+
10+
// Definition for a binary tree node.
11+
// #[derive(Debug, PartialEq, Eq)]
12+
// pub struct TreeNode {
13+
// pub val: i32,
14+
// pub left: Option<Rc<RefCell<TreeNode>>>,
15+
// pub right: Option<Rc<RefCell<TreeNode>>>,
16+
// }
17+
//
18+
// impl TreeNode {
19+
// #[inline]
20+
// pub fn new(val: i32) -> Self {
21+
// TreeNode {
22+
// val,
23+
// left: None,
24+
// right: None
25+
// }
26+
// }
27+
// }
28+
use std::cell::RefCell;
29+
use std::collections::VecDeque;
30+
use std::rc::Rc;
31+
impl Solution {
32+
pub fn zigzag_level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
33+
if root.is_none() {
34+
return vec![];
35+
}
36+
let mut q = VecDeque::new();
37+
let mut zigzag = false;
38+
let mut res = Vec::new();
39+
q.push_back(root);
40+
while !q.is_empty() {
41+
let mut v: Vec<i32> = Vec::new();
42+
let mut size = q.len();
43+
while size > 0 {
44+
if zigzag {
45+
let r = q.pop_front().unwrap().unwrap();
46+
let b = r.borrow();
47+
if !b.right.is_none() {
48+
q.push_back(b.right.clone());
49+
}
50+
if !b.left.is_none() {
51+
q.push_back(b.left.clone());
52+
}
53+
v.push(b.val);
54+
} else {
55+
let r = q.pop_back().unwrap().unwrap();
56+
let b = r.borrow();
57+
if !b.left.is_none() {
58+
q.push_front(b.left.clone());
59+
}
60+
if !b.right.is_none() {
61+
q.push_front(b.right.clone());
62+
}
63+
v.push(b.val);
64+
}
65+
size -= 1;
66+
}
67+
zigzag = !zigzag;
68+
res.push(v);
69+
}
70+
res
71+
}
72+
}
73+
74+
// solution impl ends here
75+
76+
// solution tests starts here
77+
78+
#[cfg(test)]
79+
mod tests {
80+
use super::*;
81+
82+
#[test]
83+
fn test_case0() {
84+
assert_eq!(
85+
Solution::zigzag_level_order(tree![3, 9, 20, null, null, 15, 7]),
86+
vec![vec![3], vec![20, 9], vec![15, 7]]
87+
);
88+
}
89+
}
90+
91+
// solution tests ends here

‎src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod a0035_search_insert_position;
1313
mod a0053_maximum_subarray;
1414
mod a0066_plus_one;
1515
mod a0088_merge_sorted_array;
16+
mod a0103_binary_tree_zigzag_level_order_traversal;
1617
mod a0118_pascals_triangle;
1718
mod a0119_pascals_triangle_ii;
1819
mod a0145_binary_tree_postorder_traversal;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /