-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserialize-and-deserialize-bst.rs
95 lines (82 loc) · 2.3 KB
/
serialize-and-deserialize-bst.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#![allow(dead_code, unused, unused_variables)]
fn main() {}
// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}
use std::cell::RefCell;
use std::rc::Rc;
/**
* Your Codec object will be instantiated and called as such:
* let obj = Codec::new();
* let data: String = obj.serialize(strs);
* let ans: Option<Rc<RefCell<TreeNode>>> = obj.deserialize(data);
*/
struct Codec {}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl Codec {
fn new() -> Self {
Self {}
}
/// 当为None是序列化为-1
fn serialize(&self, root: Option<Rc<RefCell<TreeNode>>>) -> String {
match root {
Some(root) => {
let value = root.borrow().val;
let left = root.borrow_mut().left.take();
let right = root.borrow_mut().right.take();
format!(
"{},{},{}",
self.serialize(left),
value,
self.serialize(right),
)
}
None => "-1".to_string(),
}
}
fn deserialize(&self, data: String) -> Option<Rc<RefCell<TreeNode>>> {
println!("{:?}", data);
let s = data
.split(',')
.into_iter()
.map(|x| x.parse().unwrap())
.collect::<Vec<i32>>();
self.f(&s[..], &mut 0)
}
fn f(&self, data: &[i32], index: &mut usize) -> Option<Rc<RefCell<TreeNode>>> {
if data.len() <= *index || data[*index] == -1 {
return None;
}
let mut node = TreeNode::new(data[*index]);
*index += 1;
if data[*index] != -1 {
node.left = self.f(data, index);
} else {
node.left = None;
}
*index += 1;
if data[*index] != -1 {
node.right = self.f(data, index);
} else {
node.right = None;
}
Some(Rc::new(RefCell::new(node)))
}
}