feat(geektime_algo): add 23 binary tree
binary tree traversal and level order traversal
This commit is contained in:
parent
a10a462d3f
commit
192a137904
61
rust/23_binary_tree/binary_tree_traversal.rs
Normal file
61
rust/23_binary_tree/binary_tree_traversal.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use super::util::tree::{TreeNode, to_tree};
|
||||
|
||||
// 前序遍历
|
||||
pub fn pre_order_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
|
||||
let mut result: Vec<i32> = vec![];
|
||||
if root.is_none() { return result; }
|
||||
|
||||
_pre_order(root, &mut result);
|
||||
result
|
||||
}
|
||||
|
||||
fn _pre_order(root: Option<Rc<RefCell<TreeNode>>>, result: &mut Vec<i32>) {
|
||||
match root {
|
||||
Some(node) => {
|
||||
result.push(node.borrow().val);
|
||||
_pre_order(node.borrow().left.clone(), result);
|
||||
_pre_order(node.borrow().right.clone(), result);
|
||||
},
|
||||
None => { return; }
|
||||
}
|
||||
}
|
||||
|
||||
// 中序遍历
|
||||
pub fn in_order_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
|
||||
let mut result: Vec<i32> = vec![];
|
||||
if root.is_none() { return result; }
|
||||
|
||||
_in_order(root, &mut result);
|
||||
result
|
||||
}
|
||||
|
||||
fn _in_order(root: Option<Rc<RefCell<TreeNode>>>, result: &mut Vec<i32>) {
|
||||
match root {
|
||||
Some(node) => {
|
||||
_in_order(node.borrow().left.clone(), result);
|
||||
result.push(node.borrow().val);
|
||||
_in_order(node.borrow().right.clone(), result);
|
||||
},
|
||||
None => { return; }
|
||||
}
|
||||
}
|
||||
|
||||
// 后续遍历
|
||||
pub fn post_order_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
|
||||
let mut result: Vec<i32> = vec![];
|
||||
if root.is_none() { return result; }
|
||||
|
||||
_post_order(root, &mut result);
|
||||
result
|
||||
}
|
||||
|
||||
fn _post_order(root: Option<Rc<RefCell<TreeNode>>>, result: &mut Vec<i32>) {
|
||||
match root {
|
||||
Some(node) => {
|
||||
_post_order(node.borrow().left.clone(), result);
|
||||
_post_order(node.borrow().right.clone(), result);
|
||||
result.push(node.borrow().val);
|
||||
},
|
||||
None => { return; }
|
||||
}
|
||||
}
|
34
rust/23_binary_tree/level_order_traversal.rs
Normal file
34
rust/23_binary_tree/level_order_traversal.rs
Normal file
@ -0,0 +1,34 @@
|
||||
// leetcode https://leetcode.com/problems/binary-tree-level-order-traversal/
|
||||
|
||||
use super::util::tree::{TreeNode, to_tree};
|
||||
use std::collections::VecDeque;
|
||||
|
||||
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
|
||||
let mut result: Vec<Vec<i32>> = vec![];
|
||||
if root.is_none() { return result; }
|
||||
|
||||
let mut deque: VecDeque<Option<Rc<RefCell<TreeNode>>>> = VecDeque::new();
|
||||
deque.push_back(root);
|
||||
|
||||
while !deque.is_empty() {
|
||||
let mut current_level = vec![];
|
||||
let mut added = false;
|
||||
let level_size = deque.len();
|
||||
|
||||
for i in 0..level_size {
|
||||
let n = deque.pop_front();
|
||||
if let Some(Some(node)) = n {
|
||||
current_level.push(node.borrow().val);
|
||||
added = true;
|
||||
if node.borrow().left.is_some() { deque.push_back(node.borrow().left.clone()); }
|
||||
if node.borrow().right.is_some() { deque.push_back(node.borrow().right.clone()); }
|
||||
}
|
||||
}
|
||||
|
||||
if !added { break; }
|
||||
|
||||
result.push(current_level);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
57
rust/23_binary_tree/util/tree.rs
Normal file
57
rust/23_binary_tree/util/tree.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
|
||||
#[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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_tree(vec: Vec<Option<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
use std::collections::VecDeque;
|
||||
let head = Some(Rc::new(RefCell::new(TreeNode::new(vec[0].unwrap()))));
|
||||
let mut queue = VecDeque::new();
|
||||
queue.push_back(head.as_ref().unwrap().clone());
|
||||
|
||||
for children in vec[1..].chunks(2) {
|
||||
let parent = queue.pop_front().unwrap();
|
||||
if let Some(v) = children[0] {
|
||||
parent.borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(v))));
|
||||
queue.push_back(parent.borrow().left.as_ref().unwrap().clone());
|
||||
}
|
||||
if children.len() > 1 {
|
||||
if let Some(v) = children[1] {
|
||||
parent.borrow_mut().right = Some(Rc::new(RefCell::new(TreeNode::new(v))));
|
||||
queue.push_back(parent.borrow().right.as_ref().unwrap().clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
head
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! tree {
|
||||
() => {
|
||||
None
|
||||
};
|
||||
($($e:expr),*) => {
|
||||
{
|
||||
let vec = vec![$(stringify!($e)), *];
|
||||
let vec = vec.into_iter().map(|v| v.parse::<i32>().ok()).collect::<Vec<_>>();
|
||||
to_tree(vec)
|
||||
}
|
||||
};
|
||||
($($e:expr,)*) => {(tree![$($e),*])};
|
||||
}
|
Loading…
Reference in New Issue
Block a user