forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cargo fmt code * Add empty line to seperate unrelated comments * Fix review * Update bubble_sort.rs * Update merge_sort.rs --------- Co-authored-by: Yudong Jin <[email protected]>
- Loading branch information
Showing
70 changed files
with
1,021 additions
and
836 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 54 additions & 50 deletions
104
codes/rust/chapter_backtracking/preorder_traversal_ii_compact.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,54 @@ | ||
/* | ||
* File: preorder_traversal_ii_compact.rs | ||
* Created Time: 2023-07-15 | ||
* Author: codingonion ([email protected]) | ||
*/ | ||
|
||
include!("../include/include.rs"); | ||
|
||
use std::{cell::RefCell, rc::Rc}; | ||
use tree_node::{vec_to_tree, TreeNode}; | ||
|
||
/* 前序遍历:例题二 */ | ||
fn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) { | ||
if root.is_none() { | ||
return; | ||
} | ||
if let Some(node) = root { | ||
// 尝试 | ||
path.push(node.clone()); | ||
if node.borrow().val == 7 { | ||
// 记录解 | ||
res.push(path.clone()); | ||
} | ||
pre_order(res, path, node.borrow().left.clone()); | ||
pre_order(res, path, node.borrow().right.clone()); | ||
// 回退 | ||
path.remove(path.len() - 1); | ||
} | ||
} | ||
|
||
/* Driver Code */ | ||
pub fn main() { | ||
let root = vec_to_tree([1, 7, 3, 4, 5, 6, 7].map(|x| Some(x)).to_vec()); | ||
println!("初始化二叉树"); | ||
print_util::print_tree(root.as_ref().unwrap()); | ||
|
||
// 前序遍历 | ||
let mut path = Vec::new(); | ||
let mut res = Vec::new(); | ||
pre_order(&mut res, &mut path, root); | ||
|
||
println!("\n输出所有根节点到节点 7 的路径"); | ||
for path in res { | ||
let mut vals = Vec::new(); | ||
for node in path { | ||
vals.push(node.borrow().val) | ||
} | ||
println!("{:?}", vals); | ||
} | ||
} | ||
/* | ||
* File: preorder_traversal_ii_compact.rs | ||
* Created Time: 2023-07-15 | ||
* Author: codingonion ([email protected]) | ||
*/ | ||
|
||
include!("../include/include.rs"); | ||
|
||
use std::{cell::RefCell, rc::Rc}; | ||
use tree_node::{vec_to_tree, TreeNode}; | ||
|
||
/* 前序遍历:例题二 */ | ||
fn pre_order( | ||
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, | ||
path: &mut Vec<Rc<RefCell<TreeNode>>>, | ||
root: Option<Rc<RefCell<TreeNode>>>, | ||
) { | ||
if root.is_none() { | ||
return; | ||
} | ||
if let Some(node) = root { | ||
// 尝试 | ||
path.push(node.clone()); | ||
if node.borrow().val == 7 { | ||
// 记录解 | ||
res.push(path.clone()); | ||
} | ||
pre_order(res, path, node.borrow().left.clone()); | ||
pre_order(res, path, node.borrow().right.clone()); | ||
// 回退 | ||
path.remove(path.len() - 1); | ||
} | ||
} | ||
|
||
/* Driver Code */ | ||
pub fn main() { | ||
let root = vec_to_tree([1, 7, 3, 4, 5, 6, 7].map(|x| Some(x)).to_vec()); | ||
println!("初始化二叉树"); | ||
print_util::print_tree(root.as_ref().unwrap()); | ||
|
||
// 前序遍历 | ||
let mut path = Vec::new(); | ||
let mut res = Vec::new(); | ||
pre_order(&mut res, &mut path, root); | ||
|
||
println!("\n输出所有根节点到节点 7 的路径"); | ||
for path in res { | ||
let mut vals = Vec::new(); | ||
for node in path { | ||
vals.push(node.borrow().val) | ||
} | ||
println!("{:?}", vals); | ||
} | ||
} |
Oops, something went wrong.