This problem looks busy at first, but the accepted solution is built around one steady invariant. For 449. Serialize and Deserialize BST, the solution in this repository is mainly a two pointers solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers, sliding window.
The notes already sitting in the source point us in the right direction:
- use upper and lower boundaries to check whether we should add null
- https://leetcode.com/problems/serialize-and-deserialize-bst/discuss/177617/the-General-Solution-for-Serialize-and-Deserialize-BST-and-Serialize-and-Deserialize-BT
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are serialize, deserialize.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The queue gives the solution a level-by-level or frontier-style traversal.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//use upper and lower boundaries to check whether we should add null
02//https://leetcode.com/problems/serialize-and-deserialize-bst/discuss/177617/the-General-Solution-for-Serialize-and-Deserialize-BST-and-Serialize-and-Deserialize-BT
03//Runtime: 40 ms, faster than 84.94% of C++ online submissions for Serialize and Deserialize BST.
04//Memory Usage: 28.7 MB, less than 5.45% of C++ online submissions for Serialize and Deserialize BST.
05/**
06 * Definition for a binary tree node.
07 * struct TreeNode {
08 * int val;
09 * TreeNode *left;
10 * TreeNode *right;
11 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
12 * };
13 */
14class Codec {
15public:
16 void serialize(TreeNode* node, string& s){
17 if(!node) return;
18
19 //preorder
20 s += to_string(node->val) + " ";
21 serialize(node->left, s);
22 serialize(node->right, s);
23 }
24
25 // Encodes a tree to a single string.
26 string serialize(TreeNode* root) {
27 string s;
28 serialize(root, s);
29 // cout << s << endl;
30 return s;
31 }
32
33 TreeNode* deserialize(queue<string>& q, int lower, int upper){
34 if(q.empty()) return nullptr;
35
36 int val = stoi(q.front());
37 // cout << "checking " << val << endl;
38 if(val < lower || val > upper) return nullptr;
39
40 //only pop the string after ensuring it can be used here
41 q.pop();
42
43 // cout << val << " is used" << endl;
44
45 TreeNode* node = new TreeNode(val);
46 // cout << val << "'s left" << endl;
47 node->left = deserialize(q, lower, val);
48 // cout << val << "'s right" << endl;
49 node->right = deserialize(q, val, upper);
50 return node;
51 }
52
53 // Decodes your encoded data to tree.
54 TreeNode* deserialize(string data) {
55 if(data.empty()) return nullptr;
56
57 istringstream ss(data);
58 queue<string> q;
59 string s;
60
61 while(ss >> s){
62 q.push(s);
63 }
64
65 return deserialize(q, INT_MIN, INT_MAX);
66 }
67};
68
69// Your Codec object will be instantiated and called as such:
70// Codec* ser = new Codec();
71// Codec* deser = new Codec();
72// string tree = ser->serialize(root);
73// TreeNode* ans = deser->deserialize(tree);
74// return ans;
75
76//use # to mark "nullptr"
77//https://leetcode.com/problems/serialize-and-deserialize-bst/discuss/177617/the-General-Solution-for-Serialize-and-Deserialize-BST-and-Serialize-and-Deserialize-BT
78//Runtime: 36 ms, faster than 93.72% of C++ online submissions for Serialize and Deserialize BST.
79//Memory Usage: 29.7 MB, less than 5.45% of C++ online submissions for Serialize and Deserialize BST.
80class Codec {
81public:
82 void serialize(TreeNode* node, string& s){
83 if(!node){
84 s += "# ";
85 return;
86 }
87
88 //preorder
89 s += to_string(node->val) + " ";
90 serialize(node->left, s);
91 serialize(node->right, s);
92 }
93
94 // Encodes a tree to a single string.
95 string serialize(TreeNode* root) {
96 string s;
97 serialize(root, s);
98 // cout << s << endl;
99 return s;
100 }
101
102 TreeNode* deserialize(queue<string>& q, int lower, int upper){
103 if(q.front() == "#"){
104 // cout << "null" << endl;
105 q.pop();
106 return nullptr;
107 }
108
109 int val = stoi(q.front()); q.pop();
110 // cout << val << endl;
111
112 TreeNode* node = new TreeNode(val);
113 // cout << val << "'s left" << endl;
114 node->left = deserialize(q, lower, val);
115 // cout << val << "'s right" << endl;
116 node->right = deserialize(q, val, upper);
117 return node;
118 }
119
120 // Decodes your encoded data to tree.
121 TreeNode* deserialize(string data) {
122 if(data.empty()) return nullptr;
123
124 istringstream ss(data);
125 queue<string> q;
126 string s;
127
128 while(ss >> s){
129 q.push(s);
130 }
131
132 return deserialize(q, INT_MIN, INT_MAX);
133 }
134};
Cost