问题: 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.

Example: 

You may serialize the following tree:

    1
/ \
2 3
/ \
4 5 as "[1,2,3,null,null,4,5]"

Clarification: The above format is the same as how LeetCode 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: // Encodes a tree to a single string.
string serialize(TreeNode* root) {
if(root == NULL){
return "#";
}
return to_string(root->val) + ","+serialize(root->left)+","+serialize(root->right);
} // Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if(data == "#")return NULL;
stringstream s(data);
return helper(s);
} TreeNode* helper(stringstream& s){
string temp;
getline(s,temp,','); if(temp == "#"){
return NULL;
}else{
TreeNode* root = new TreeNode(stoi(temp));
root->left = helper(s);
root->right= helper(s); //stoi(str, 0, n); //将字符串 str 从 0 位置开始到末尾的 n 进制转换为十进制 return root;
}
} }; // Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));

说明:

stringstream

是 C++ 提供的另一个字串型的串流(stream)物件,和 iostream、fstream 有类似的操作方式。要使用 stringstream, 必須先加入這一行:#include <sstream>。

第28行,使用 stringstream s(data) 将string类型的data转换为 stringstream类型的s,传递到helper函数里。问题是,既然我们要的是string,那么为什么不直接转换?其实目的是为了分割,原来的string是类似的格式 ”1,2,3,NULL,NULL,4“,所有的数字都是通过逗号隔开,如果要在c++中进行拆分,那么进行转化比较方便。

转化过后,使用 getline(s,temp,','); 。 以 逗号 为分界,将第一个逗号之前的内容,存入temp。

stoi

然后,你可以看到stoi函数。功能是:将 n 进制的字符串转化为十进制。

int a = stoi(str, , );
/*
0是从0位开始
2是2进制
默认是10进制,所以这道题目直接填入string 数字就好
return是int型
*/

再谈 string 和 char

在其它的地方曾看到 string 和 char 的对比,比较直观清晰,就贴在这里作为记录。

操作 string char列表
初始化 string s; char s[100];

获取第i个字节

s[i] s[i]
字符串长度 s.length() or s.size() strlen(s)
读取一行 getline(cin,s) gets(s)
设定某一行 s = "KYK" strcpy(s,"KYK")
字符串相加

s = s+ "KYK";

s += "KYK"

strcat(s,"KYK")
字符串比较 s == "KYK" strcmp(s,"KYK")

出处:https://www.twblogs.net/a/5b80fc902b71772165aa71f4

LC 297 Serialize and Deserialize Binary Tree的更多相关文章

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

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

  2. 297. Serialize and Deserialize Binary Tree二叉树的序列化和反序列化(就用Q)

    [抄题]: Serialization is the process of converting a data structure or object into a sequence of bits ...

  3. 297. Serialize and Deserialize Binary Tree

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

  4. 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 ...

  5. [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 ...

  6. 297. Serialize and Deserialize Binary Tree *HARD*

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

  7. 【LeetCode】297. Serialize and Deserialize Binary Tree

    二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...

  8. 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化

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

  9. Leetcode 297. Serialize and Deserialize Binary Tree

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

随机推荐

  1. nginx+keepalived高可用实战

    1.整体架构图如下 2.环境准备 今天所配置的是keepalived+nginx 的负载均衡 下载keepalived软件 [root@LB01 tools]# wget http://www.kee ...

  2. IDEA中新建子模块

    在IDEA中新建子模块简单步骤: 找到父模块 ->new Module ,然后: next之后,输入ArtifactId: next之后,再输入子模块名,其中,要注意,在contentRoot和 ...

  3. Ubuntu+QEMU+Xv6环境搭建

    操作系统:Ubuntu 16.04 32位 虚拟机:VMware 模拟器:QEMU 之前有一台centos 64位虚拟机,使用源码安装配置环境,出了一些列问题,最终环境都已经配好了,也能够在qemu上 ...

  4. Centos7 中查找文件、目录、内容

    1.查找文件 find / -name ‘filename’ 2.查找目录 find / -name ‘path’ -type d 3.查找内容 find . | xargs grep -ri ‘co ...

  5. comparison of truncate vs delete in mysql/sqlserver

    comparison of truncate vs delete in mysql/sqlserver [duplicate]   DELETE DELETE is a DML Command. DE ...

  6. 右键查看别人网页的js代码为什么会显示乱码

    查看别人网页的js显示乱码 解决方法: 打开浏览器,选择设置,点击更多,选择文字编码为Unicode

  7. linux postgresql

  8. 批量管理工具:pssh/ansible

    ssh 免密码 批量管理1.创建用户useradd user1echo “123456”| passwd --stdin user12.创建秘钥ssh-keygen -t dsa然后一直回车 非交互式 ...

  9. python 设计模式之适配器模式 Adapter Class/Object Pattern

    #写在前面 看完了<妙味>和<华医>,又情不自禁的找小说看,点开了推荐里面随机弹出的<暗恋.橘生淮南>,翻了下里面的评论,有个读者从里面摘了一段自己很喜欢的话出来, ...

  10. Linux Nginx naxsi

    nginx naxsi 模块 - 简书https://www.jianshu.com/p/8492da04b3ba naxsi compile · nbs-system/naxsi Wikihttps ...