当前位置:网站首页>297. 二叉树的序列化与反序列化
297. 二叉树的序列化与反序列化
2022-07-31 00:48:00 【anieoo】
原题链接:297. 二叉树的序列化与反序列化

solution
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if(root == NULL) {
return "_#";
}
string res = "_" + to_string(root->val);
res += serialize(root->left);
res += serialize(root->right);
return res;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if(data == "_#") return NULL;
queue<string> q;
int n = data.size();
for(int i = 0;i < n;i++) {
int j = i;
string item;
while(j < n && data[j] != '_') item += data[j++];
i = j;
if(item != "") q.push(item);
}
return helper(q);
}
TreeNode* helper(queue<string>& q)
{
string val = q.front();
q.pop();
if (val == "#")
return NULL;
TreeNode* head = new TreeNode(stoi(val));
head->left = helper(q);
head->right = helper(q);
return head;
}
};
// Your Codec object will be instantiated and called as such:
// Codec ser, deser;
// TreeNode* ans = deser.deserialize(ser.serialize(root));边栏推荐
- Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch
- MySQL高级-六索引优化
- Jetpack Compose learning (8) - State and remeber
- MySQL database (basic)
- This project is so geeky
- Optimization of aggregate mentioned at DATA AI Summit 2022
- typescript10-commonly used basic types
- 【愚公系列】2022年07月 Go教学课程 016-运算符之逻辑运算符和其他运算符
- Consistency and Consensus of Distributed Systems (1) - Overview
- Redis learning
猜你喜欢

MySQL高级-六索引优化
![Xss target drone training [success when pop-up window is realized]](/img/c1/61280bddc9a42a3827735dc31c86e5.png)
Xss target drone training [success when pop-up window is realized]

Jmeter parameter transfer method (token transfer, interface association, etc.)

typescript14-(单独指定参数和返回值的类型)

什么是Promise?Promise的原理是什么?Promise怎么用?

不用Swagger,那我用啥?

Typescript14 - (type) of the specified parameters and return values alone

Can deep learning solve the parameters of a specific function?

Oracle has a weird temporary table space shortage problem

ShardingSphere之水平分库实战(四)
随机推荐
MySQL grant statements
MySQL triggers
【Yugong Series】July 2022 Go Teaching Course 013-Constants, Pointers
The difference between truncate and delete in MySQL database
MySQL系列一:账号管理与引擎
Typescript14 - (type) of the specified parameters and return values alone
WEB安全基础 - - -漏洞扫描器
DOM系列之scroll系列
Summary of MySQL database interview questions (2022 latest version)
GO GOPROXY proxy Settings
typescript16-void
华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
MySQL——数据库的查,增,删
MySQL数据库面试题总结(2022最新版)
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
registers (assembly language)
Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
【Yugong Series】July 2022 Go Teaching Course 019-For Circular Structure
Shell programming of conditional statements
ShardingSphere之读写分离(八)