当前位置:网站首页>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
边栏推荐
- 智能视觉组比赛流程草案
- 一个注解优雅的实现 接口数据脱敏
- Wechat applet development Basics
- Mobaihe box, ZTE box, Migu box, Huawei box, Huawei Yuehe box, Fiberhome box, Skyworth box, Tianyi box and other operators' box firmware collection and sharing
- leetcode:304. 2D area and retrieval - matrix immutable
- Data statistical analysis (SPSS) [7]
- 【FPGA+sin】基于DDS(直接数字合成)的正弦信号发生器模块FPGA实现
- [tcapulusdb knowledge base] batch copy the game area
- Data collection and management [11]
- [tcaplusdb knowledge base] Introduction to tcaplusdb tcaplusadmin tool
猜你喜欢

迅为i.MX8M开发板yocto系统使用Gstarwmr视频转换

Ugui slider minimum control

人大金仓(KingBase)导出表结构

Set hardware breakpoint instruction for ejtag under the PMON of the Godson development board

SqlServer如何查询除去整列字段为null的结果
![[tcaplusdb knowledge base] view tcapdir directory server](/img/b6/f3734dfb03ec789525636335457b99.png)
[tcaplusdb knowledge base] view tcapdir directory server

Efficientnetv2 - get smaller models and faster training with NAS, scaling, and fused mbconv

SQL performance optimization is really eye popping
![[World Ocean Day] tcapulusdb calls on you to protect marine biodiversity together](/img/87/373af42f3a2ffa6b9f7fb0c0c3735b.png)
[World Ocean Day] tcapulusdb calls on you to protect marine biodiversity together

Ask the handler about the memory leak scenario in the interview. Don't just know static internal classes & weak references!
随机推荐
sql两列变为多行过滤显示
Data collection and management [10]
一个注解优雅的实现 接口数据脱敏
迅为i.MX8M开发板yocto系统使用Gstarwmr视频转换
Data collection and management [15]
云原生周报 | Grafana 9正式发布;云原生词汇表中文版现已上线
go-redsync分布式锁源码解析
An annotation elegant implementation of interface data desensitization
高性能限流器 Guava RateLimiter
Idea of importing point cloud map into gazebo
Yangzhou needs one English IT Helpdesk Engineer -20220216
人大金仓(KingBase)导出表结构
Data statistical analysis (SPSS) [3]
[Brillouin phenomenon] Study on simultaneous measurement system of Brillouin temperature and strain distribution in optical fiber
Live broadcast preview | neurips special session I & Young Scientists special session
Distributed ID solution
Source code analysis of go redsync distributed lock
2022年 6月27号 《暑假感悟篇一》路程的选择权。
The second meeting of the Second Council of Euler open source community was held, and Xinhua III, hyperfusion and Godson Zhongke became members of the Council
Use gstarwmr video conversion for yocto system of i.mx8m development board