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.

这题实际上思路还是比较清晰的,遇见NULL的话那么存入一个#既可以了,解序列化的时候直接相应的处理#就可以了,代码如下:

 /**
* 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: // Encodes a tree to a single string.
string serialize(TreeNode* root) {
queue<TreeNode *> q;
string str = "";
if(!root) return str;
stringstream ss;
ss << root->val;
str += ss.str();
q.push(root);
while(!q.empty()){
TreeNode * t = q.front();
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
if(t->left){
ss.str("");
ss << "," << t->left->val;
str += ss.str();
}else{
ss.str("");
ss << "," << "#";
str += ss.str();
} if(t->right){
ss.str("");
ss << "," << t->right->val;
str += ss.str();
}else{
ss.str("");
ss << "," << "#";
str += ss.str();
}
q.pop();
}
//删除末尾的#字符
for(int i = str.length() - ; ; ){
if(str[i] == '#'){
str = str.substr(, i - );
i = str.length() - ;
}else{
break;
}
}
return str;
} // Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if(!data.size()) return NULL;
vector<string> dataVec;
int sz = data.length();
for(int i = ; i < sz;){//分割字符串,保存到vector中
if(data[i] != ','){
int j = i;
for(; j < sz && data[j] != ','; ++j)
;
string tmp = data.substr(i, j - i);//注意substr的使用细节
dataVec.push_back(tmp);
i = j + ; //跳过','符号
}
}
sz = dataVec.size();
TreeNode * root = new TreeNode(atoi(dataVec[].c_str()));
queue<TreeNode *>q;
q.push(root);
int i = ;
while(!q.empty() && i < sz){
TreeNode * t = q.front();
if(dataVec[i] != "#"){
t->left = new TreeNode(atoi(dataVec[i].c_str()));
q.push(t->left);
}else{
t->left = NULL;
}
i++;
if(i >= sz) break;
if(dataVec[i] != "#"){
t->right = new TreeNode(atoi(dataVec[i].c_str()));
q.push(t->right);
}else{
t->right = NULL;
}
q.pop();
i++;
}
return root;
}
};

吐槽一下,c++连个split函数都没有,写起来真是恶心,还要自己去分割字符串。

LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)的更多相关文章

  1. [leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  2. Leetcode 297. Serialize and Deserialize Binary Tree

    https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...

  3. [LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  4. [leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码

    由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...

  5. [LeetCode] Serialize and Deserialize Binary Tree

    Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...

  6. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  7. LeetCode 428. Serialize and Deserialize N-ary Tree

    原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree/ 题目: Serialization is the ...

  8. LC 297 Serialize and Deserialize Binary Tree

    问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...

  9. [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)

    描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...

  10. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. 关于服务器jdk版本和代码编译调试兼容问题

    首先代码是基于哪个版本编写和调试,有没有用到新版本jdk新的特性,类啊接口啊啥的,用到了的话,就不行了 其他都共有的是向下兼容的 最好开发环境的jdk版本和部署环境的jdk版本匹配.

  2. 1.6 使用电脑测试MC20的读取带中文短信功能

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  3. Shell字符串操作

    @1:子串削除 ${string#substring} 从$string 的开头位置截掉最短匹配的$substring. ${string##substring} 从$string 的开头位置截掉最长 ...

  4. sharepoint 2010自定义访问日志列表设置移动终端否和客户端访问系统等计算列的公式

    上个月本人开发和上线了一个在SharePoint 2010上基于HTML5的移动OA网站,后端服务采用自定义的基于AgilePoint工作流引擎的Sharepoint Web服务,前端主要采用Jque ...

  5. iOS 52个技巧学习心得笔记 第二章 对象 , 消息, 运行期

    1. 属性 在开发过程中经常要用到定义属性,@property和@synthesize是经常用到的属性, property在.h文件中作声明,@synthesize在.m文件中用于实现 // Stud ...

  6. JQuery Div层滚动条控制(模拟横向滚动条在最顶端显示)

    想让DIV层滚动条显示在顶端,CSS样式没找到相关属性,于是用2个DIV层来模拟做了一个.经测试IE浏览器上显示并不太美观!不知道是否还有更好的办法可以实现这功能呢?   aaaaaaasssssss ...

  7. 【HackerRank】Median

    题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...

  8. 面向过程编程实例------grep-rl 'root 路径

    #应用:grep -rl 'root' /etc import os def deco(func): def wrapper(*args): g=func(*args) next(g) return ...

  9. 20145231《Java程序设计》第四次实验报告

    实验四 Android开发基础 实验内容 •安装Android Studio •运行安卓AVD模拟器 •使用Android运行出模拟手机并显示自己的学号 实验步骤 一.安装Android Studio ...

  10. INSPIRED启示录 读书笔记 - 第9章 产品副经理

    发现帮手 从本质上讲,产品就是创意,产品经理的职责是想出好点并加以实现.我们需要好点子,有些想法是我们自己的创意,但如果仅依靠自己,就会严重限制创意的发挥 做产品要找公司最聪明的人合作,发现公司里潜在 ...