-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbinary-tree-zigzag-level-order-traversal.rs
82 lines (77 loc) · 2.61 KB
/
binary-tree-zigzag-level-order-traversal.rs
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
// 103. Binary Tree Zigzag Level Order Traversal
// 🟠 Medium
//
// https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
//
// Tags: Tree - Breadth-First Search - Binary Tree
use std::cell::RefCell;
use std::rc::Rc;
struct Solution;
impl Solution {
// Use a breadth-first traversal combined with a flag to determine which
// levels need to be reversed. Keep a stack that contains an entire level
// at a time, first get the values of the current level and append them
// to the result, then use the nodes to obtain the next level.
//
// Time complexity: O(n) - We will visit every node and do constant work
// for each.
// Space complexity: O(n) - The stack will hold one level of the tree at
// any given point.
//
// Runtime 1 ms Beats 68.42%
// Memory 2.2 MB Beats 10.53%
pub fn zigzag_level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
match root {
None => vec![],
Some(root) => {
let mut res: Vec<Vec<i32>> = vec![];
let mut stack: Vec<Rc<RefCell<TreeNode>>> = vec![root];
let mut reverse_level = false;
while stack.len() > 0 {
let values: Vec<i32> = if reverse_level {
stack.iter().rev().map(|r| r.borrow().val).collect()
} else {
stack.iter().map(|r| r.borrow().val).collect()
};
res.push(values);
reverse_level = !reverse_level;
let mut next_level: Vec<Rc<RefCell<TreeNode>>> = vec![];
for r in stack {
match &r.borrow().left {
Some(cr) => next_level.push(cr.clone()),
None => (),
}
match &r.borrow().right {
Some(cr) => next_level.push(cr.clone()),
None => (),
}
}
stack = next_level;
}
res
}
}
}
}
// Tests.
fn main() {
// assert_eq!(Solution::min_diff_in_bst(root), 1);
println!("All tests passed!")
}
// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}