Idiomatic rust
This commit is contained in:
parent
b0d6ac58f1
commit
282d882d53
@ -10,7 +10,7 @@ use std::{cell::RefCell, rc::Rc};
|
|||||||
use tree_node::{vec_to_tree, TreeNode};
|
use tree_node::{vec_to_tree, TreeNode};
|
||||||
|
|
||||||
/* 前序遍历:例题一 */
|
/* 前序遍历:例题一 */
|
||||||
fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {
|
fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<&Rc<RefCell<TreeNode>>>) {
|
||||||
if root.is_none() {
|
if root.is_none() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -19,8 +19,8 @@ fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeN
|
|||||||
// 记录解
|
// 记录解
|
||||||
res.push(node.clone());
|
res.push(node.clone());
|
||||||
}
|
}
|
||||||
pre_order(res, node.borrow().left.clone());
|
pre_order(res, node.borrow().left.as_ref());
|
||||||
pre_order(res, node.borrow().right.clone());
|
pre_order(res, node.borrow().right.as_ref());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ pub fn main() {
|
|||||||
|
|
||||||
// 前序遍历
|
// 前序遍历
|
||||||
let mut res = Vec::new();
|
let mut res = Vec::new();
|
||||||
pre_order(&mut res, root);
|
pre_order(&mut res, root.as_ref());
|
||||||
|
|
||||||
println!("\n输出所有值为 7 的节点");
|
println!("\n输出所有值为 7 的节点");
|
||||||
let mut vals = Vec::new();
|
let mut vals = Vec::new();
|
||||||
|
@ -13,7 +13,7 @@ use tree_node::{vec_to_tree, TreeNode};
|
|||||||
fn pre_order(
|
fn pre_order(
|
||||||
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
|
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
|
||||||
path: &mut Vec<Rc<RefCell<TreeNode>>>,
|
path: &mut Vec<Rc<RefCell<TreeNode>>>,
|
||||||
root: Option<Rc<RefCell<TreeNode>>>,
|
root: Option<&Rc<RefCell<TreeNode>>>,
|
||||||
) {
|
) {
|
||||||
if root.is_none() {
|
if root.is_none() {
|
||||||
return;
|
return;
|
||||||
@ -25,10 +25,10 @@ fn pre_order(
|
|||||||
// 记录解
|
// 记录解
|
||||||
res.push(path.clone());
|
res.push(path.clone());
|
||||||
}
|
}
|
||||||
pre_order(res, path, node.borrow().left.clone());
|
pre_order(res, path, node.borrow().left.as_ref());
|
||||||
pre_order(res, path, node.borrow().right.clone());
|
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 path = Vec::new();
|
||||||
let mut res = 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 的路径");
|
println!("\n输出所有根节点到节点 7 的路径");
|
||||||
for path in res {
|
for path in res {
|
||||||
|
@ -13,7 +13,7 @@ use tree_node::{vec_to_tree, TreeNode};
|
|||||||
fn pre_order(
|
fn pre_order(
|
||||||
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
|
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
|
||||||
path: &mut 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 {
|
if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {
|
||||||
@ -26,10 +26,10 @@ fn pre_order(
|
|||||||
// 记录解
|
// 记录解
|
||||||
res.push(path.clone());
|
res.push(path.clone());
|
||||||
}
|
}
|
||||||
pre_order(res, path, node.borrow().left.clone());
|
pre_order(res, path, node.borrow().left.as_ref());
|
||||||
pre_order(res, path, node.borrow().right.clone());
|
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 path = Vec::new();
|
||||||
let mut res = 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 的节点");
|
println!("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点");
|
||||||
for path in res {
|
for path in res {
|
||||||
|
Loading…
Reference in New Issue
Block a user