2023-07-26 09:15:58 +08:00
|
|
|
/*
|
2023-03-07 23:46:28 +08:00
|
|
|
* File: tree_node.rs
|
|
|
|
* Created Time: 2023-02-27
|
2023-07-26 09:15:58 +08:00
|
|
|
* Author: xBLACKICEx (xBLACKICE@outlook.com), night-cruise (2586447362@qq.com)
|
2023-03-07 23:46:28 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2023-07-26 09:15:58 +08:00
|
|
|
/* 二叉树节点类型 */
|
2023-07-16 01:32:43 +08:00
|
|
|
#[derive(Debug)]
|
2023-03-07 23:46:28 +08:00
|
|
|
pub struct TreeNode {
|
|
|
|
pub val: i32,
|
2023-07-16 01:32:43 +08:00
|
|
|
pub height: i32,
|
2023-03-07 23:46:28 +08:00
|
|
|
pub parent: Option<Rc<RefCell<TreeNode>>>,
|
|
|
|
pub left: Option<Rc<RefCell<TreeNode>>>,
|
|
|
|
pub right: Option<Rc<RefCell<TreeNode>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeNode {
|
2023-07-26 09:15:58 +08:00
|
|
|
/* 构造方法 */
|
2023-03-07 23:46:28 +08:00
|
|
|
pub fn new(val: i32) -> Rc<RefCell<Self>> {
|
|
|
|
Rc::new(RefCell::new(Self {
|
|
|
|
val,
|
2023-07-16 01:32:43 +08:00
|
|
|
height: 0,
|
2023-03-07 23:46:28 +08:00
|
|
|
parent: None,
|
|
|
|
left: None,
|
2024-04-07 14:46:21 +08:00
|
|
|
right: None,
|
2023-03-07 23:46:28 +08:00
|
|
|
}))
|
|
|
|
}
|
2023-04-21 14:58:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! op_vec {
|
|
|
|
( $( $x:expr ),* ) => {
|
|
|
|
vec![
|
2024-04-07 14:46:21 +08:00
|
|
|
$( Option::from($x).map(|x| x) ),*
|
2023-04-21 14:58:46 +08:00
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-26 09:15:58 +08:00
|
|
|
// 序列化编码规则请参考:
|
|
|
|
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
|
|
|
// 二叉树的数组表示:
|
|
|
|
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
|
|
|
// 二叉树的链表表示:
|
|
|
|
// /——— 15
|
|
|
|
// /——— 7
|
|
|
|
// /——— 3
|
|
|
|
// | \——— 6
|
|
|
|
// | \——— 12
|
|
|
|
// ——— 1
|
|
|
|
// \——— 2
|
|
|
|
// | /——— 9
|
|
|
|
// \——— 4
|
|
|
|
// \——— 8
|
|
|
|
|
|
|
|
/* 将列表反序列化为二叉树:递归 */
|
|
|
|
fn vec_to_tree_dfs(arr: &[Option<i32>], i: usize) -> Option<Rc<RefCell<TreeNode>>> {
|
|
|
|
if i >= arr.len() || arr[i].is_none() {
|
2023-04-21 14:58:46 +08:00
|
|
|
return None;
|
|
|
|
}
|
2023-07-26 09:15:58 +08:00
|
|
|
let root = TreeNode::new(arr[i].unwrap());
|
|
|
|
root.borrow_mut().left = vec_to_tree_dfs(arr, 2 * i + 1);
|
|
|
|
root.borrow_mut().right = vec_to_tree_dfs(arr, 2 * i + 2);
|
|
|
|
Some(root)
|
|
|
|
}
|
2023-04-21 14:58:46 +08:00
|
|
|
|
2023-07-26 09:15:58 +08:00
|
|
|
/* 将列表反序列化为二叉树 */
|
|
|
|
pub fn vec_to_tree(arr: Vec<Option<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
|
|
|
|
vec_to_tree_dfs(&arr, 0)
|
|
|
|
}
|
2023-04-21 14:58:46 +08:00
|
|
|
|
2023-07-26 09:15:58 +08:00
|
|
|
/* 将二叉树序列化为列表:递归 */
|
|
|
|
fn tree_to_vec_dfs(root: Option<Rc<RefCell<TreeNode>>>, i: usize, res: &mut Vec<Option<i32>>) {
|
|
|
|
if root.is_none() {
|
|
|
|
return;
|
2024-04-07 14:46:21 +08:00
|
|
|
}
|
2023-07-26 09:15:58 +08:00
|
|
|
let root = root.unwrap();
|
2024-04-07 14:46:21 +08:00
|
|
|
// i + 1 is the minimum valid size to access index i
|
|
|
|
while res.len() < i + 1 {
|
2023-07-26 09:15:58 +08:00
|
|
|
res.push(None);
|
2023-04-21 14:58:46 +08:00
|
|
|
}
|
2023-07-26 09:15:58 +08:00
|
|
|
res[i] = Some(root.borrow().val);
|
|
|
|
tree_to_vec_dfs(root.borrow().left.clone(), 2 * i + 1, res);
|
|
|
|
tree_to_vec_dfs(root.borrow().right.clone(), 2 * i + 2, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 将二叉树序列化为列表 */
|
|
|
|
pub fn tree_to_vec(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Option<i32>> {
|
|
|
|
let mut res = vec![];
|
|
|
|
tree_to_vec_dfs(root, 0, &mut res);
|
|
|
|
res
|
|
|
|
}
|