问题: 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. 分享7个shell脚本实例--shell脚本练习必备

    概述 看多shell脚本实例自然就会有shell脚本的编写思路了,所以我一般比较推荐看脚本实例来练习shell脚本.下面分享几个shell脚本实例. 1.监测Nginx访问日志502情况,并做相应动作 ...

  2. 纯JavaScript开发飞机大战项目

    开发工具: HBuilder 编程语言:JavaScript 其他技术:Html + Css 项目截图:   视频:   源代码:   在线观看地址: (暂无) 百度网盘下载地址: 请加QQ群:915 ...

  3. select,poll,epoll最简单的解释

    从事服务端开发,少不了要接触网络编程.epoll 作为 Linux 下高性能网络服务器的必备技术至关重要,nginx.Redis.Skynet 和大部分游戏服务器都使用到这一多路复用技术. epoll ...

  4. !!a标签和button按钮只允许点击一次,防止重复提交

    button 方法:加上属性disabled = “disabled” 或者 disabled = “true” <button id="btn" disabled=&quo ...

  5. Java基础 main 参数String[] args的用法

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  6. pip安装各种模块

    date: 2019-08-20   19:27:09 pip install requests pip install xpinyin pip install redis

  7. Mac OS 安装 MySQL5.7

    在 macOS 上安装 MySQL 5.7 安装 Homebrew $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubuserconten ...

  8. PHP 范围解析操作符 (::) 主要用于访问静态成员和类常量

    范围解析操作符 (::) 范围解析操作符(也可称作 Paamayim Nekudotayim)或者更简单地说是一对冒号,可以用于访问静态成员,类常量,还可以用于覆盖类中的属性和方法. 当在类定义之外引 ...

  9. git merge 结果是 git merge Already up-to-date. 该怎么解决?

    git将主干合并到当前分支时,出现如下结果: 原因在于:执行git merge前,主干的代码没有更新 正确的操作步骤如下: 1 .切换到主干 $ git checkout master 2. 更新主干 ...

  10. Game and Application Protocol

    This privacy policy details the information collected by the team ("we" or "our" ...