449. Serialize and Deserialize BST Medium

@problem@discussion
#String#Tree#Depth-First Search#Breadth-First Search#Design#Binary Search Tree#Binary Tree



1/**
2 * [449] Serialize and Deserialize BST
3 *
4 * Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
5 * Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You need to ensure that a binary search tree can be serialized to a string, and this string can be deserialized to the original tree structure.
6 * The encoded string should be as compact as possible.
7 *  
8 * Example 1:
9 * Input: root = [2,1,3]
10 * Output: [2,1,3]
11 * Example 2:
12 * Input: root = []
13 * Output: []
14 *  
15 * Constraints:
16 * 
17 * 	The number of nodes in the tree is in the range [0, 10^4].
18 * 	0 <= Node.val <= 10^4
19 * 	The input tree is guaranteed to be a binary search tree.
20 * 
21 */
22pub struct Solution {}
23use crate::util::tree::{TreeNode, to_tree};
24
25// problem: https://leetcode.com/problems/serialize-and-deserialize-bst/
26// discuss: https://leetcode.com/problems/serialize-and-deserialize-bst/discuss/?currentPage=1&orderBy=most_votes&query=
27
28// submission codes start here
29
30// Definition for a binary tree node.
31// #[derive(Debug, PartialEq, Eq)]
32// pub struct TreeNode {
33//   pub val: i32,
34//   pub left: Option<Rc<RefCell<TreeNode>>>,
35//   pub right: Option<Rc<RefCell<TreeNode>>>,
36// }
37// 
38// impl TreeNode {
39//   #[inline]
40//   pub fn new(val: i32) -> Self {
41//     TreeNode {
42//       val,
43//       left: None,
44//       right: None
45//     }
46//   }
47// }
48use std::rc::Rc;
49use std::cell::RefCell;
50struct Codec {
51	
52}
53
54/** 
55 * `&self` means the method takes an immutable reference.
56 * If you need a mutable reference, change it to `&mut self` instead.
57 */
58impl Codec {
59    fn new() -> Self {
60        Some(Rc::new(RefCell::new(TreeNode::new(0))))
61    }
62
63    fn serialize(&self, root: Option<Rc<RefCell<TreeNode>>>) -> String {
64        
65    }
66	
67    fn deserialize(&self, data: String) -> Option<Rc<RefCell<TreeNode>>> {
68        
69    }
70}
71
72/**
73 * Your Codec object will be instantiated and called as such:
74 * let obj = Codec::new();
75 * let data: String = obj.serialize(strs);
76 * let ans: Option<Rc<RefCell<TreeNode>>> = obj.deserialize(data);
77 */
78
79// submission codes end
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn test_449() {
87    }
88}
89


Back
© 2025 bowen.ge All Rights Reserved.