请实现两个函数,分别用来序列化和反序列化二叉树。
您需要确保二叉树可以序列化为字符串,并且可以将此字符串反序列化为原始树结构。
样例
你可以序列化如下的二叉树
8
/ \
12 2
/ \
6 4
为:"[8, 12, 2, null, null, 6, 4, null, null, null, null]"
注意:
- 以上的格式是AcWing序列化二叉树的方式,你不必一定按照此格式,所以可以设计出一些新的构造方式。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
string serialize(TreeNode* root) {
string res;
//if (!root) return res;
dfs_s(root, res);
return res;
}
void dfs_s(TreeNode* root, string & res) {
if (!root) {
res += "null ";//这个是null加空格。。。。
return;
}
res += to_string(root->val) + ' ';
dfs_s(root->left, res);
dfs_s(root->right, res);
}
TreeNode* deserialize(string data) {
int u = 0;
return dfs_d(data, u);
}
TreeNode * dfs_d(string data, int &u) {
if(u == data.size()) return NULL;
int k = u ;
while(data[k] != ' ') k++;
if (data[u] == 'n') {
u = k + 1;
return NULL;
}
int sum = 0;
for (int i = u; i < k; i ++) {
sum = sum * 10 + data[i] - '0';
}
u = k + 1;
auto root = new TreeNode(sum);
root->left = dfs_d(data, u);
root->right = dfs_d(data, u);
return root;
}
};