2024-03-16 02:13:41 +08:00
|
|
|
/*
|
|
|
|
* File: preorder_traversal_iii_compact.rs
|
|
|
|
* Created Time: 2023-07-15
|
|
|
|
* Author: codingonion (coderonion@gmail.com)
|
|
|
|
*/
|
|
|
|
|
2024-10-23 18:49:47 +08:00
|
|
|
use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
|
2024-03-16 02:13:41 +08:00
|
|
|
use std::{cell::RefCell, rc::Rc};
|
|
|
|
|
|
|
|
/* 前序遍历:例题三 */
|
|
|
|
fn pre_order(
|
|
|
|
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
|
|
|
|
path: &mut Vec<Rc<RefCell<TreeNode>>>,
|
2024-05-24 16:21:17 +08:00
|
|
|
root: Option<&Rc<RefCell<TreeNode>>>,
|
2024-03-16 02:13:41 +08:00
|
|
|
) {
|
|
|
|
// 剪枝
|
|
|
|
if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if let Some(node) = root {
|
|
|
|
// 尝试
|
|
|
|
path.push(node.clone());
|
|
|
|
if node.borrow().val == 7 {
|
|
|
|
// 记录解
|
|
|
|
res.push(path.clone());
|
|
|
|
}
|
2024-05-24 16:21:17 +08:00
|
|
|
pre_order(res, path, node.borrow().left.as_ref());
|
|
|
|
pre_order(res, path, node.borrow().right.as_ref());
|
2024-03-16 02:13:41 +08:00
|
|
|
// 回退
|
2024-05-24 16:21:17 +08:00
|
|
|
path.pop();
|
2024-03-16 02:13:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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();
|
2024-05-24 16:21:17 +08:00
|
|
|
pre_order(&mut res, &mut path, root.as_ref());
|
2024-03-16 02:13:41 +08:00
|
|
|
|
|
|
|
println!("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点");
|
|
|
|
for path in res {
|
|
|
|
let mut vals = Vec::new();
|
|
|
|
for node in path {
|
|
|
|
vals.push(node.borrow().val)
|
|
|
|
}
|
|
|
|
println!("{:?}", vals);
|
|
|
|
}
|
|
|
|
}
|