mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-23 08:47:12 +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() || root.as_ref().unwrap().borrow().val == 3 {
|
||||
@@ -26,10 +26,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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,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 的路径,路径中不包含值为 3 的节点");
|
||||
for path in res {
|
||||
|
||||
Reference in New Issue
Block a user