-
Notifications
You must be signed in to change notification settings - Fork 0
/
level_order_traversal.rs
94 lines (73 loc) · 2.4 KB
/
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
83
84
85
86
87
88
89
90
91
92
93
94
// 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,
}
}
}
use std::rc::Rc;
use std::{cell::RefCell, collections::VecDeque};
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
let mut output: Vec<Vec<i32>> = vec![];
match root {
Some(ref the_root) => {
let mut queue: VecDeque<Rc<RefCell<TreeNode>>> = VecDeque::new();
queue.push_back(Rc::clone(the_root));
while !queue.is_empty() {
let mut queue_len = queue.len();
let mut current_level: Vec<i32> = vec![];
while queue_len > 0 {
queue_len -= 1;
let current = queue.pop_front();
match current {
Some(ref the_current) => {
let the_node = the_current.as_ref().borrow();
if let Some(left) = &the_node.left {
queue.push_back(Rc::clone(left));
}
if let Some(right) = &the_node.right {
queue.push_back(Rc::clone(right));
}
current_level.push(the_node.val);
}
None => {}
}
}
output.push(current_level);
}
}
None => {}
}
return output;
}
pub fn recursive_level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
type Node = Rc<RefCell<TreeNode>>;
let mut output: Vec<Vec<i32>> = vec![];
fn traverse(node: &Node, output: &mut Vec<Vec<i32>>, depth: usize) {
let n = node.borrow();
if output.len() == depth {
output.push(Vec::new())
}
output[depth].push(n.val);
if let Some(l) = &n.left {
traverse(&l, output, depth + 1)
}
if let Some(r) = &n.right {
traverse(&r, output, depth + 1)
}
}
if let Some(root) = root {
traverse(&root, &mut output, 0);
}
output
}