mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-24 00:57:14 +00:00
[Rust] make rust part more idomatic and fix panic of backtrack template (#1370)
* Drop unused variable * Idiomatic rust * Fix panic template
This commit is contained in:
@@ -13,7 +13,7 @@ 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>>>,
|
||||
root: Option<&Rc<RefCell<TreeNode>>>,
|
||||
) {
|
||||
if root.is_none() {
|
||||
return;
|
||||
@@ -25,10 +25,10 @@ fn pre_order(
|
||||
// 记录解
|
||||
res.push(path.clone());
|
||||
}
|
||||
pre_order(res, path, node.borrow().left.clone());
|
||||
pre_order(res, path, node.borrow().right.clone());
|
||||
pre_order(res, path, node.borrow().left.as_ref());
|
||||
pre_order(res, path, node.borrow().right.as_ref());
|
||||
// 回退
|
||||
path.remove(path.len() - 1);
|
||||
path.pop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ pub fn main() {
|
||||
// 前序遍历
|
||||
let mut path = Vec::new();
|
||||
let mut res = Vec::new();
|
||||
pre_order(&mut res, &mut path, root);
|
||||
pre_order(&mut res, &mut path, root.as_ref());
|
||||
|
||||
println!("\n输出所有根节点到节点 7 的路径");
|
||||
for path in res {
|
||||
|
||||
Reference in New Issue
Block a user