当前位置:网站首页>二叉树序列化与反序列化(leetcode(困难))
二叉树序列化与反序列化(leetcode(困难))
2022-06-29 03:36:00 【努力变好的zz】
前言
一、题目详解
题目链接
1.先序后序层序序列化
#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();
}
总结
解题关键在于怎样序列化的就怎样反序列化回去
边栏推荐
- Live broadcast preview | neurips special session I & Young Scientists special session
- Same tree [from part to whole]
- 二叉树的层序遍历 II[层序遍历方式之一 ->递归遍历 + level]
- [ruoyi] ztree initialization
- 【TcaplusDB知识库】修改业务修改集群cluster
- Preliminary construction of SSM project environment
- Common methods of JS date and time
- 【面试指南】AI算法面试
- 2D human posture estimation deeppose
- Open source demo| you draw and I guess -- make your life more interesting
猜你喜欢
![[tcapulusdb knowledge base] tcapulusdb technical support introduction](/img/12/83dd224d41551b9137b4e986ea8e63.png)
[tcapulusdb knowledge base] tcapulusdb technical support introduction
![[interview guide] AI algorithm interview](/img/00/918608afe7f2b8e4fd89770b512b41.png)
[interview guide] AI algorithm interview

Use gstarwmr video conversion for yocto system of i.mx8m development board

MATALB signal processing - signal transformation (7)

Geth --- Error: authentication needed: password or unlock

Ugui slider minimum control
![二叉树的锯齿形层序遍历[分层遍历方式之一 -> 前序遍历+level]](/img/f6/0df9f2a454cea0a95a5347546a90fb.png)
二叉树的锯齿形层序遍历[分层遍历方式之一 -> 前序遍历+level]

The four traversal methods of the map set can be seen at a glance

87.(cesium篇)cesium热力图(贴地形)

Get error: Unsupported fork ordering: eip150block not enabled, but eip155block enabled at 0
随机推荐
Data statistical analysis (SPSS) [7]
搭建nexus服务
Implementing mqtt communication with PHP
Unable to locate program input point [email protected]
Go implements distributed locks
Basic concepts of graph theory
【资料上新】基于3568开发板的NPU开发资料全面升级
Do you feel confused when you study at three in the morning?
Potential learning C language - pointer explanation (Advanced)
【TcaplusDB知识库】批量复制游戏区
Preliminary construction of SSM project environment
Inventory deduction based on redis
[tcapulusdb knowledge base] Introduction to tcapulusdb table data caching
Different binary search trees [bottom-up backtracking spanning tree + memory search -- space for time]
VIM configuration and use
Why is informatization ≠ digitalization? Finally someone made it clear
go-redsync分布式锁源码解析
Same tree [from part to whole]
leetcode:560. 和为 K 的子数组
[ruoyi] ztree initialization