Leetcode 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
For example, you may serialize the following tree
1
/
2 3
/
4 5
as "[1,2,3,null,null,4,5]"
, just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
二叉树的序列化与反序列化。
其实就是通过某一种遍历方法将树变成序列存储,再将序列还原成树。用一个非数字的字符表示空节点,比如“#”
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
void dfs(string &path, TreeNode* root)
{
if(!root)
{
path += "#";
return ;
}
path += to_string(root->val) + "#";
dfs(path, root->left);
dfs(path, root->right);
}
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string res;
dfs(res, root);
return res;
}
TreeNode* dfs2(string &data)
{
int pos = data.find("#");
if(!pos)
{
data = data.substr(pos + 1);
return NULL;
}
TreeNode* root = new TreeNode(stoi(data.substr(0, pos)));
data = data.substr(pos + 1);
root->left = dfs2(data);
root->right = dfs2(data);
return root;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
string data2 = data;
return dfs2(data2);
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- MySQL information_schema详解 PLUGINS
- 不到 20 行 Python 代码即可制作精美证件照
- 不到 100 行 Python 代码即可实现换脸功能
- 53 道 Python 面试题,帮你成为大数据工程师
- 用 Python 将 QQ 好友头像生成「 五一快乐」
- Jenkins集成Github自动部署
- Linux&树莓派安装seafile私有云盘
- 推荐一款可视化+NoteBook工具
- 用 Python 送上特殊的母亲节祝福
- 还在为 520 发愁吗?教你用 Python 写个表白神器!
- 用 Python 实现微信自动回复
- 一行命令给猎狐 F4 带口罩 & 检测是否带口罩
- 备战 618,用 Python 领取京东优惠券
- 一键生成你的微信社交数据报告
- 分析OutOfMemoryError异常