二叉树序列化与反序列化(leetcode(困难))

3 篇文章 0 订阅
3 篇文章 0 订阅


前言

一、题目详解

题目链接
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();
}

总结

解题关键在于怎样序列化的就怎样反序列化回去

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1无名之辈1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值