题目

序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。

请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。

示例: 

你可以将以下二叉树:

    1
/ \
2 3
/ \
4 5 序列化为 "[1,2,3,null,null,4,5]"

提示: 这与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。

说明: 不要使用类的成员 / 全局 / 静态变量来存储状态,你的序列化和反序列化算法应该是无状态的。


Tag

IO库,stringstream的读取。

两个方法:stringstream in(str) :string往流里存。in.str() 从流返回string。 dfs。string -> int :stoi(val)。


法1.

1.序列化:dfs前中后、bfs层次。反序列化要和序列化方法对应,结果唯一。

2.空节点:‘#’

3.每个节点结束标志:‘ ’,作用是防止歧义

4.用std的string流来对string进行分割,in>>val;每次遇到空格就停止,流的状态改变。

//dfs:preOrder
class Codec {
public: // Encodes a tree to a single string.
string serialize(TreeNode* root) {
ostringstream out;
serialize(root,out);
return out.str();
} // Decodes your encoded data to tree.
TreeNode* deserialize(string data)
{
istringstream in(data);
return deserialize(in);
} private:
// root - left - right
void serialize(TreeNode *root,ostringstream &out)
{
if(root)
{
out<<root->val<<' ';//‘ ’为结束符
serialize(root->left,out);
serialize(root->right,out);
}
else
{
out<<"# ";//#代表空节点,后面还有一个结束符' '
}
} TreeNode* deserialize(istringstream &in)
{
string val;
in >> val; //一个单词作为独立元素。用string流把string分割了
if(val=="#")
return nullptr;
TreeNode* root=new TreeNode(stoi(val));
root->left=deserialize(in);
root->right=deserialize(in);
return root;
}
};

法2.BFS.队列。根节点入queue。检查根不为空。将根的左右节点入queue。

//BFS
class Codec {
public: // Encodes a tree to a single string.
string serialize(TreeNode* root) { if(!root)
return ""; queue<TreeNode*> q;
q.push(root);
stringstream out;//string 流,初始化接受string定义,int输入用<< while(!q.empty())
{
TreeNode* t = q.front();
q.pop();
if(t)//bfs如果根节点存在,把左右孩子依次压入队列
{
out<< t->val <<" ";
q.push(t->left);
q.push(t->right);
}
else
{
out<<"# ";
}
}
cout<<out.str();
return out.str();
} // Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if(data=="")
return nullptr;
queue<TreeNode*> myque;
stringstream in(data);
string val;
in>>val;
//第一个单词一定存在,压入队列,且是根节点
TreeNode* root=new TreeNode(stoi(val));
TreeNode* cur=root;
myque.push(root); while(!myque.empty())
{
cur=myque.front();myque.pop();
in>>val;
if(val!="#")
{
cur->left=new TreeNode(stoi(val));
myque.push(cur->left);
} in>>val;
if(val!="#")
{
cur->right=new TreeNode(stoi(val));
myque.push(cur->right);
} }
return root;
}
};

法3 返回char* 的做法。 strdup(out.str().c_str()); 将string 转为char *. 反之可以直接转换。

//bfs
#include <string.h>
class Solution {
public:
char* Serialize(TreeNode *root) {
if(!root)
return nullptr;
stringstream out;
queue<TreeNode*> myque;
myque.push(root); while(!myque.empty())
{
root=myque.front();myque.pop();
if(root)
{
out<<root->val<<" ";
myque.push(root->left);
myque.push(root->right);
}
else
{
out<<"# ";
}
}
return strdup(out.str().c_str());
} TreeNode* Deserialize(char *str) {
if(!str)
return nullptr;
string s=str;
stringstream in(s);
queue<TreeNode*> myque;
string val;
in>>val;
TreeNode* ret = new TreeNode(stoi(val));
TreeNode* root =ret;
myque.push(root);
while(!myque.empty())
{
root= myque.front();
myque.pop();
in>>val;
if(val!="#")
{
root->left = new TreeNode(stoi(val));
myque.push(root->left);
}
in>>val;
if(val!="#")
{
root->right = new TreeNode(stoi(val));
myque.push(root->right);
}
}
return ret;
}
};

问题

1.atoi/stoi/strtoi/stoui区别

2.C++ IO库

3.string 转 char*

头文件:#include <string.h>

定义函数:char * strdup(const char *s);

函数说明:strdup()会先用maolloc()配置与参数s 字符串相同的空间大小,然后将参数s 字符串的内容复制到该内存地址,然后把该地址返回。该地址最后可以利用free()来释放。

返回值:返回一字符串指针,该指针指向复制后的新字符串地址。若返回NULL 表示内存不足。

#include <string.h>
main(){
    char a[] = "strdup";
    char *b;
    b = strdup(a);
    printf("b[]=\"%s\"\n", b);
}

LeetCode297. Serialize and Deserialize Binary Tree的更多相关文章

  1. [LeetCode] Serialize and Deserialize Binary Tree

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

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

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

  3. LC 297 Serialize and Deserialize Binary Tree

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

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

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

  5. [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree

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

  6. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

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

  7. LeetCode——Serialize and Deserialize Binary Tree

    Description: Serialization is the process of converting a data structure or object into a sequence o ...

  8. Serialize and Deserialize Binary Tree

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

  9. 297. Serialize and Deserialize Binary Tree

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

随机推荐

  1. python单元测试框架-unittest(四)之用例综合框架管理

    简述为何如要框架? 前面测试用例与执行都是写在一个文件,当用例数量不断增加的时候,用例的执行与管理变得非常麻烦,因此需要对用例根据具体的功能模块来使用单独的模块来管理.就像一所学校要根据不同年级进行分 ...

  2. Sean McGinnis

    * Loaded log from Wed Nov 25 22:19:43 2015 * Now talking on #openstack-smaug* [smcginnis] (~smcginni ...

  3. jemeter接口测试基础

    前言: 本文主要针对http接口进行测试,使用Jmeter工具实现. Jmter工具设计之初是用于做性能测试的,它在实现对各种接口的调用方面已经做的比较成熟,因此,本次直接使用Jmeter工具来完成对 ...

  4. Java反射机制分析指南

    一.JAVA是动态语言吗? 一般而言,说到动态言,都是指在程序运行时允许改变程序结构或者变量类型,从这个观点看,JAVA和C++一样,都不是动态语言. 但JAVA它却有着一个非常突出的动态相关机制:反 ...

  5. Unity 滚轮实现UGUI ScrollView的缩放

    本文原创,转载请注明出处http://www.cnblogs.com/AdvancePikachu/p/7908754.html 前段时间在做一个类似AnimationCurve的可视化编辑器,其中在 ...

  6. 2602 最短路径问题Dihstra算法

    题目描述 Description 平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间.其中的一些点之间有连线.若有连线,则表示可从一个点到达另一个点,即两点间有通路,通 ...

  7. url长度

    今天在测试Email Ticket的时候发现在进行Mark as Read/Unread操作时,请求是通过GET方式进行的.URL中列出了所有参与该操作的Ticket Id.于是,我想起GET请求是有 ...

  8. tween.js 插件

    1.是什么? jQueryTween是一款轻量级的jQuery补间动画工具库插件.使用jQueryTween可以制作出各种平滑的动画过渡效果.该插件基于tween.js,旨在简化各种补间动画操作,提供 ...

  9. 增加ssh无密码信任连接的安全性

    为了方便系统管理或者服务器运维自动化,我们通常要在服务器间做ssh无密码信任连接. 环境:目标主机    centos7    192.168.150.110操作主机    centos7-cn 19 ...

  10. Angular JS + Express JS入门搭建网站

    3月份开始,接到了新的任务,跟UI开发有关,用的是Angular JS,Express JS等技术.于是周末顺便学习下新技术. 组里产品UI架构如下: 其中前端,主要使用Angular JS框架,另外 ...