Merge pull request #364 from caitlingao/23_binary_tree

feat(geektime_algo): add 23 binary tree
This commit is contained in:
wangzheng0822 2019-07-27 23:04:18 +08:00 committed by GitHub
commit 86e039ab7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 227 additions and 0 deletions

View File

@ -0,0 +1,44 @@
// leetcode: https://leetcode.com/problems/binary-tree-inorder-traversal/
use super::util::tree::{TreeNode, to_tree};
// 中序遍历Recursive Approach
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut result: Vec<i32> = vec![];
if root.is_none() { return result; }
_inorder(root, &mut result);
result
}
fn _inorder(root: Option<Rc<RefCell<TreeNode>>>, result: &mut Vec<i32>) {
match root {
Some(node) => {
_inorder(node.borrow().left.clone(), result);
result.push(node.borrow().val);
_inorder(node.borrow().right.clone(), result);
},
None => { return; }
}
}
// 中序遍历Iterating method using Stack
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut result = vec![];
if root.is_none() { return result; }
let mut stack: Vec<Rc<RefCell<TreeNode>>> = Vec::new();
let mut r = root.clone();
while r.is_some() || !stack.is_empty() {
while let Some(node) = r {
stack.push(node.clone());
r = node.borrow().left.clone();
}
r = stack.pop();
if let Some(node) = r {
result.push(node.borrow().val);
r = node.borrow().right.clone();
}
}
result
}

View 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
}

View File

@ -0,0 +1,48 @@
// https://leetcode.com/problems/binary-tree-postorder-traversal/
use super::util::tree::{TreeNode, to_tree};
// 后续遍历Recursive Approach
pub fn postorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut result: Vec<i32> = vec![];
if root.is_none() { return result; }
_postorder(root, &mut result);
result
}
fn _postorder(root: Option<Rc<RefCell<TreeNode>>>, result: &mut Vec<i32>) {
match root {
Some(node) => {
_postorder(node.borrow().left.clone(), result);
_postorder(node.borrow().right.clone(), result);
result.push(node.borrow().val);
},
None => { return; }
}
}
// 后序遍历Iterating method using Stack
pub fn postorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut result = vec![];
if root.is_none() { return result; }
let mut stack1: Vec<Option<Rc<RefCell<TreeNode>>>> = Vec::new();
let mut stack2: Vec<Option<Rc<RefCell<TreeNode>>>> = Vec::new();
stack1.push(root);
while let Some(Some(node)) = stack1.pop() {
if node.borrow().left.is_some() {
stack1.push(node.borrow().left.clone());
}
if node.borrow().right.is_some() {
stack1.push(node.borrow().right.clone());
}
stack2.push(Some(node));
}
while let Some(Some(node)) = stack2.pop() {
result.push(node.borrow().val);
}
result
}

View File

@ -0,0 +1,44 @@
//leetcode: https://leetcode.com/problems/binary-tree-preorder-traversal/
use super::util::tree::{TreeNode, to_tree};
// 前序遍历Recursive Approach
pub fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut result: Vec<i32> = vec![];
if root.is_none() { return result; }
_preorder(root, &mut result);
result
}
fn _preorder(root: Option<Rc<RefCell<TreeNode>>>, result: &mut Vec<i32>) {
match root {
Some(node) => {
result.push(node.borrow().val);
_preorder(node.borrow().left.clone(), result);
_preorder(node.borrow().right.clone(), result);
},
None => { return; }
}
}
// 前序遍历Iterating method using Stack
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut result = vec![];
if root.is_none() { return result; }
let mut stack: Vec<Rc<RefCell<TreeNode>>> = Vec::new();
let mut r = root.clone();
while r.is_some() || !stack.is_empty() {
while let Some(node) = r {
result.push(node.borrow().val);
stack.push(node.clone());
r = node.borrow().left.clone();
}
r = stack.pop();
if let Some(node) = r {
r = node.borrow().right.clone();
}
}
result
}

View 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),*])};
}