二叉树的序列化与反序列化。

如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便。

这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流'ostringstream'.

istringstream in;

in >> str;

这里没执行一次就会倒出一个string (因为in流中使用了' '空格 作为分割符, 所以可以分成很多个string)

建树的时候使用先序建立二叉树。

关键代码如下:

TreeNode* build(istringstream in){
if(#) return null;
else{
new a node
newnode -> left = build(in)
newnode -> right = build(in)
}
}
/**
* 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) {
ostringstream out;
backtrack(root, out);
return out.str();
} // Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
istringstream in(data);
return build(in);
} private:
void backtrack(TreeNode *rt, ostringstream &out){
if(rt){
out << rt -> val << " ";
backtrack(rt -> left, out);
backtrack(rt -> right, out);
}else{
out << "#"<<" ";
}
} // 形如: 1 # 2 其中树节点必为满树空节点用#表示。中间用空格分割。
TreeNode* build(istringstream &in){
string str = "";
in >> str;
if(str == "#" || str == ""){
return NULL;
}else{
TreeNode *rt = new TreeNode(atoi(str.c_str()));
rt -> left = build(in);
rt -> right = build(in);
return rt;
}
}
}; // Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));

【LeetCode】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. 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 ...

  3. 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)

    [LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...

  4. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  5. LC 297 Serialize and Deserialize Binary Tree

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

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

  7. Leetcode 297. Serialize and Deserialize Binary Tree

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

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

  9. 【leetcode】449. Serialize and Deserialize BST

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

随机推荐

  1. 【Mongodb教程 第十一课 】MongoDB 聚合

    聚合操作过程中的数据记录和计算结果返回.聚合操作分组值从多个文档,并可以执行各种操作,分组数据返回单个结果.在SQL COUNT(*)和group by 相当于MongoDB的聚集. aggregat ...

  2. 多媒体开发之---h264 server rtsp

    (1)live555 (2)gstreamer http://code.openhub.net/search?s=rtsp%20server (3)srs (4)ffmpeg

  3. 识别IE11浏览器

    现在俺们做的系统十分高大上,用IE的话非要上IE11或以上版本. 咋检测呢?检测到用户用IE.且IE低于IE11的话就提示他升级浏览器呢?可以酱紫: var _IE = (function (d, w ...

  4. 2016/4/1 PDO:: 数据访问抽象层 ? :

    ①PDO方式连接 数据库 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  5. noteexpress使用指南

    软件功能:在写论文时直接调用参考数据并输出正规的格式. (以下简称NE) A.下载安装 下载地址:Note-express - Bibliography Software  选择相应的学校进行下载,相 ...

  6. Koa2学习(三)GET请求

    Koa2学习(三)GET请求 GET请求是前后端交互最常用的请求之一,常常用来进行查询操作. 那么Koa是如何接收并处理GET请求呢? 创建一个服务 // 引入Koa const Koa = requ ...

  7. ECharts 使用

    最近项目中要做图形报表,要求使用echarts实现,图形报表有很多中实现之前也接触过,但echarts还是头一次听说,正好可以趁这个机会好好学习一下它. 之前不知道就不知道啦,现在知道了就了不得了,一 ...

  8. socket.io实现在线群聊

    我自己在用socket.io开发,对官方网站上的文档,进行简单的整理,然后自己写了一个简单的聊天程序.最最开始 先安装socket.io: npm install socket.io 利用Node的搭 ...

  9. 添加和删除节点(HTML元素)

    创建新的HTML元素 <div id="div1"> <p id="p1">这是一个段落</p> <p id=&quo ...

  10. EF1:MVC/EF(Entity Framewok) /First Migrations

    1. 概念 Entity Framework: ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案.(此处 ...