当前位置:网站首页>Binary tree serialization and deserialization (leetcode (difficult))
Binary tree serialization and deserialization (leetcode (difficult))
2022-06-29 03:54:00 【Try to be better zz】
List of articles
Preface
One 、 Topic explanation
Topic link
1. Sequence serialization after sequence
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<list>
#include<string>
#include<queue>
using std::cout;
using std::cin;
using std::endl;
using std::list;
using std::string;
using std::to_string;
using std::queue;
using std::pair;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {
}
};
struct Codec//,ǰ
{
string str;
void _ToString(TreeNode* root)
{
if (root == nullptr)
{
str += "null,";
return;
}
_ToString(root->left);
_ToString(root->right);
str += to_string(root->val) + ',';
}
string serialize(TreeNode* root) {
_ToString(root);
return str;
}
TreeNode* BulidTree(list<string>& h)
{
if (h.back() == "null")
{
h.pop_back();//front
return nullptr;
}
string f = h.back();//front
TreeNode* root = new TreeNode(stoi(f));
h.pop_back();
root->right = BulidTree(h);//root->left
root->left = BulidTree(h);//root->right
return root;
}
TreeNode* deserialize(string data) {
list<string> h = list<string>();
string tmp = "";
auto it = data.begin();
while (it != data.end())
{
if (*it == ',')
{
h.push_back(tmp);
tmp.clear();
}
else
{
tmp += *it;
}
it++;
}
return BulidTree(h);
}
};
struct Codec//
{
string str;
void _ToString(TreeNode* root)
{
if (root == nullptr)
{
str += "null,";
return;
}
queue<TreeNode*> q;
q.push(root);
str += to_string(root->val)+',';
while (!q.empty())
{
TreeNode* cur= q.front();
q.pop();
if (cur->left)
{
q.push(cur->left);
str += to_string(cur->left->val) + ',';
}
else
{
str += "null,";
}
if (cur->right)
{
q.push(cur->right);
str += to_string(cur->right->val) + ',';
}
else
{
str += "null,";
}
}
}
string serialize(TreeNode* root) {
_ToString(root);
return str;
}
TreeNode* BulidNode(string& s)
{
if (s == "null")
{
return nullptr;
}
return new TreeNode(stoi(s));
}
TreeNode* BulidTree(list<string>& h)
{
if (h.front() == "null")
{
return nullptr;
}
queue<TreeNode*> q;
TreeNode* root = BulidNode(h.front());
h.pop_front();
q.push(root);
while (!q.empty())
{
while (!h.empty())
{
TreeNode* cur = q.front();
q.pop();
if (h.front() == "null")
{
cur->left = nullptr;
h.pop_front();
}
else
{
cur->left = BulidNode(h.front());
q.push(cur->left);
h.pop_front();
}
if (h.front() == "null")
{
cur->right = nullptr;
h.pop_front();
}
else
{
cur->right = BulidNode(h.front());
q.push(cur->right);
h.pop_front();
}
}
}
return root;
}
TreeNode* deserialize(string data) {
list<string> h = list<string>();
string tmp = "";
auto it = data.begin();
while (it != data.end())
{
if (*it == ',')
{
h.push_back(tmp);
tmp.clear();
}
else
{
tmp += *it;
}
it++;
}
return BulidTree(h);
}
};
void test()
{
TreeNode* node1 = new TreeNode(1);
TreeNode* node2 = new TreeNode(2);
TreeNode* node3 = new TreeNode(3);
TreeNode* node4 = new TreeNode(4);
TreeNode* node5 = new TreeNode(5);
node1->left = node2;
node1->right = node3;
node3->left = node4;
node3->right = node5;
Codec G;
string S = G.serialize(node1);
Codec H;
cout << S << endl;
TreeNode* TR = H.deserialize(S);
}
int main()
{
test();
}
summary
The key to solving the problem is to deserialize what is serialized
边栏推荐
- 一个注解优雅的实现 接口数据脱敏
- 【TcaplusDB知识库】查看tcapdir目录服务器
- Wechat applet development Basics
- 88. (cesium chapter) cesium aggregation diagram
- 20款IDEA 神级插件 效率提升 30 倍,写代码必备
- Use gstarwmr video conversion for yocto system of i.mx8m development board
- 【布里渊现象】光纤布里渊温度和应变分布同时测量系统研究
- [tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (II)
- [tcapulusdb knowledge base] batch copy the game area
- The efficiency of 20 idea divine plug-ins has been increased by 30 times, and it is necessary to write code
猜你喜欢

leetcode:304. 2D area and retrieval - matrix immutable

Why are you a test / development programmer? Can you recall

【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(三)

欧拉开源社区第二届理事会第二次会议召开,新华三、超聚变和龙芯中科成为理事会成员单位

ssm项目环境初步搭建

基于可变参模板实现的线程池

微秒级 TCP 时间戳
![[FPGA mathematical formula] use FPGA to realize common mathematical formulas](/img/b9/e6f219738b106a96b0f5323ee61cca.png)
[FPGA mathematical formula] use FPGA to realize common mathematical formulas

Source code analysis of go redsync distributed lock

Tech Cloud Report: Mixed Office B side: How Safety and Efficiency can be combined?
随机推荐
Data statistical analysis (SPSS) [6]
sql两列变为多行过滤显示
云原生周报 | Grafana 9正式发布;云原生词汇表中文版现已上线
mysql varcahr 转 int
分享 60 个神级 VS Code 插件
数据库和缓存如何保持一致性
Preliminary construction of SSM project environment
The four traversal methods of the map set can be seen at a glance
Draft competition process of Intelligent Vision Group
【TcaplusDB知识库】TcaplusDB技术支持介绍
Establishment of small and medium-sized enterprise network
【TcaplusDB知识库】修改业务修改集群cluster
Four distributed session solutions
Data collection and management [8]
【TcaplusDB知识库】TcaplusDB-tcaplusadmin工具介绍
泠静的想一想自己的路
中小型企业网络的组建
MobileOne: 移动端仅需1ms的高性能骨干
Idea of importing point cloud map into gazebo
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(一)