1.二叉树的序列化

输入的一棵树:

//二叉树的先序遍历-序列化

#include <iostream>
#include <string>
#include <sstream> using namespace std; struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {} //构造函数
}; class Solution {
public:
string treeToStr(TreeNode* head) {
if (head == NULL) {
return "#_";
}
string res = int2str(head->val) + "_";
res += treeToStr(head->left);
res += treeToStr(head->right);
return res;
}
//int to string
string int2str(const int &int_temp)
{
stringstream stream;
stream << int_temp;
string string_temp = stream.str(); //此处也可以用 stream>>string_temp
return string_temp;
}
}; int main() {
//生成一棵二叉树
TreeNode* head = new TreeNode();
head->left = new TreeNode();
head->right = new TreeNode();
head->left->right = new TreeNode();
head->right->left = new TreeNode(); Solution test;
string s = test.treeToStr(head);
for (auto c : s) {
cout << c;
}
cout << endl;
return 0;

2.二叉树的反序列化

2.1 上面采用的先序遍历序列化,方便再反序列化回来:

    • 字符串的第一个结点就是根结点;
    • 先序遍历的非递归写法,用栈容易实现;
class Solution {
public:
TreeNode* strToTree(string s) {
//判空
int sz = s.size();
if (sz == ) return NULL; //先序遍历,借助栈
stack<TreeNode*> stack;
int idx = ;
TreeNode* head = new TreeNode((c2i(s[idx])));
idx = idx + ;//跳过‘_’分隔符 TreeNode* cur = head;
while ((cur || !stack.empty() )&& idx<sz) {
//借助栈,先序遍历
if (cur) {
stack.push(cur);
cur->left = gNode(s[idx]);
idx += ;//跳过‘_’分隔符
cur = cur->left;
}
else {
cur = stack.top();
cur->right = gNode(s[idx]);
idx += ;
stack.pop();
cur = cur->right;
}
} return head;
} //generate TreeNode
TreeNode* gNode(char a) {
if (a == '#')
return NULL;
else {
return new TreeNode(c2i((a))); }
} //char to int
int c2i(const char &char_temp)
{
stringstream stream;
stream << char_temp;
int int_temp;
stream >> int_temp;
return int_temp;
}
};

2.2 测试的完整代码,通过字符串还原成树,再输出为字符串,判断是否正确

//二叉树的反序列化-先序遍历

#include <iostream>
#include <string>
#include <sstream>
#include <stack> using namespace std; struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {} //构造函数
}; class Solution1{
public:
string serial_Of_Tree(TreeNode* head) {
if (head == NULL) {
return "#_";
}
string res = int2str(head->val) + "_";
res += serial_Of_Tree(head->left);
res += serial_Of_Tree(head->right);
return res;
}
//int to string
string int2str(const int &int_temp)
{
stringstream stream;
stream << int_temp;
string string_temp = stream.str(); //此处也可以用 stream>>string_temp
return string_temp;
}
}; class Solution {
public:
TreeNode* strToTree(string s) {
//判空
int sz = s.size();
if (sz == ) return NULL; //
stack<TreeNode*> stack;
int idx = ;
TreeNode* head = new TreeNode((c2i(s[idx])));
idx = idx + ; TreeNode* cur = head;
while ((cur || !stack.empty() )&& idx<sz) { if (cur) {
stack.push(cur);
cur->left = gNode(s[idx]);
idx += ;
cur = cur->left;
}
else {
cur = stack.top();
cur->right = gNode(s[idx]);
idx += ;
stack.pop();
cur = cur->right;
}
} return head;
} //generate TreeNode
TreeNode* gNode(char a) {
if (a == '#')
return NULL;
else {
return new TreeNode(c2i((a))); }
} //char to int
int c2i(const char &char_temp)
{
stringstream stream;
stream << char_temp;//stream作为中转站,另外使用<<和>>
int int_temp;
stream >> int_temp;
return int_temp;
}
}; int main() {
string s = "3_1_#_2_#_#_5_4_#_#_#_";
Solution test1;
TreeNode* t= test1.strToTree(s);
Solution1 test;
string s2 = test.serial_Of_Tree(test1.strToTree(s)); for (auto a : s2) {
cout << a;
}
cout << endl;
return ;
}

参考资料:

1.二叉树的序列化和反序列化  (层次遍历,用队列实现的反序列化,java)

2.C++中int、string等常见类型转换

最新文章

  1. python 数据类型 ----字典
  2. jetty for linux 启用日志
  3. 设计模式之Singleton
  4. ajax跨域 自定义header问题总结
  5. Maven本地安装JAR包组件
  6. MyEclipse背景色不伤眼+字体大小调节+代码格式化不换行
  7. gpgcheck
  8. Web弹框类
  9. 神坑 关于&amp;&amp;的取值
  10. CORS
  11. java.lang.ClassCastException: java.lang.NoClassDefFoundError cannot be cast to java.lang.RuntimeException
  12. Android - 错: java.lang.IllegalStateException: Already attached
  13. LeetCode(29)-Plus One
  14. Python常用高级函数
  15. VS2017+EF+Mysql生成实体数据模型(解决闪退的坑) 版本对应才行
  16. java中的悲观锁和乐观锁实现
  17. springboot之使用@ConfigurationProperties注解
  18. spring boot xml与dao 映射关系
  19. LR安装No Background bmp defined in section General entry BGBmp的解决办法
  20. thinking java

热门文章

  1. 孵化器使用Office365的场景及收益
  2. ServiceStack.Ormlit sqlserver枚举类型映射字段类型为varchar
  3. 第一次接触FPGA至今,总结的宝贵经验
  4. 第一章 Java Web应用开发技术
  5. java — 值传递和引用传递
  6. 团队作业7——第二次项目冲刺(Beta版本)-第三篇
  7. LintCode-54.转换字符串到整数
  8. LintCode-532.逆序对
  9. .net下将exe改造为既能双击独立运行又能注册为windows服务
  10. MySQL加密算法