diff --git a/rust/23_binary_tree/binary_tree_traversal.rs b/rust/23_binary_tree/binary_tree_traversal.rs new file mode 100644 index 0000000..95e33ad --- /dev/null +++ b/rust/23_binary_tree/binary_tree_traversal.rs @@ -0,0 +1,61 @@ +use super::util::tree::{TreeNode, to_tree}; + +// 前序遍历 +pub fn pre_order_traversal(root: Option>>) -> Vec { + let mut result: Vec = vec![]; + if root.is_none() { return result; } + + _pre_order(root, &mut result); + result +} + +fn _pre_order(root: Option>>, result: &mut Vec) { + 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>>) -> Vec { + let mut result: Vec = vec![]; + if root.is_none() { return result; } + + _in_order(root, &mut result); + result +} + +fn _in_order(root: Option>>, result: &mut Vec) { + 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>>) -> Vec { + let mut result: Vec = vec![]; + if root.is_none() { return result; } + + _post_order(root, &mut result); + result +} + +fn _post_order(root: Option>>, result: &mut Vec) { + 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; } + } +} diff --git a/rust/23_binary_tree/level_order_traversal.rs b/rust/23_binary_tree/level_order_traversal.rs new file mode 100644 index 0000000..9fb8ae3 --- /dev/null +++ b/rust/23_binary_tree/level_order_traversal.rs @@ -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>>) -> Vec> { + let mut result: Vec> = vec![]; + if root.is_none() { return result; } + + let mut deque: VecDeque>>> = 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 +} diff --git a/rust/23_binary_tree/util/tree.rs b/rust/23_binary_tree/util/tree.rs new file mode 100644 index 0000000..fc8c727 --- /dev/null +++ b/rust/23_binary_tree/util/tree.rs @@ -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>>, + pub right: Option>>, +} + +impl TreeNode { + #[inline] + pub fn new(val: i32) -> Self { + TreeNode { + val, + left: None, + right: None + } + } +} + +pub fn to_tree(vec: Vec>) -> Option>> { + 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::().ok()).collect::>(); + to_tree(vec) + } + }; + ($($e:expr,)*) => {(tree![$($e),*])}; +}