feat(geektime_algo): 23 binary tree split inorder, preorder and
postorder traversal
This commit is contained in:
parent
192a137904
commit
6154a44c84
@ -1,61 +0,0 @@
|
||||
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; }
|
||||
}
|
||||
}
|
44
rust/23_binary_tree/inorder_traversal.rs
Normal file
44
rust/23_binary_tree/inorder_traversal.rs
Normal 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
|
||||
}
|
48
rust/23_binary_tree/postorder_traversal.rs
Normal file
48
rust/23_binary_tree/postorder_traversal.rs
Normal 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
|
||||
}
|
44
rust/23_binary_tree/preorder_traversal.rs
Normal file
44
rust/23_binary_tree/preorder_traversal.rs
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user