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

如果使用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. HDU OJ Max sum 题目1003

     #include <iostream> #include<stdio.h> #include<stdlib.h> using namespace std; i ...

  2. Eclipse导入项目: No projects are found to import

    Eclipse导入项目: No projects are found to import  如果发导入工程import的时候,出现”No projects are found to import” 的 ...

  3. 修正iOS从照相机和相册中获取的图片 方向

    修正iOS从照相机和相册中获取的图片 方向   修正iOS从照相机和相册中获取的图片 方向 使用系统相机拍照得到的图片的默认方向有时不是ImageOrientationDown,而是ImageOrie ...

  4. 3 Angular 2 快速上手启动项目Demo

    Angular2.x与Angular1.x 的区别类似 Java 和 JavaScript 或者说是雷锋与雷峰塔的区别,想要运行Angular2需要安装一些第三方依赖,不会像Angular1.x那样, ...

  5. makefile redefinition or previous definition

    operation.h:4: error: redefinition of 'class operation' operation.h:5: error: previous definition of ...

  6. 辛星浅析一次ajax的实现过程

    说到ajax,那绝对是一个老生常谈的话题,近些年ajax技术的使用颇为盛行. 以下我们就以jQuery为例来从一个真实的项目中看一下ajax的实例. 首先是前端页面,这个页面我们使用的是bootstr ...

  7. C项目实践--网络协议和套接字编程

    1.TCP/IP协议 TCP/IP协议是一组包括TCP协议和IP协议,UDP(User Datagram Protocol)协议,ICMP(Internet Control Message Proto ...

  8. XML简单介绍及举例

    可扩展标记语言(eXtensibleMarkup Language,简称XML).是一种标记语言.标记指计算机所能理解的信息符号.通过此种标记,计算机之间能够处理包括各种信息的文章等. 怎样定义这些标 ...

  9. GoodUI:页面布局的技巧和设计理念

    http://goodui.org/ 中文翻译:http://www.cnblogs.com/Wayou/p/goodui.html 一年了,小小少年从幼年期过渡到成长期要开始加速冲刺了.毕竟钻头就是 ...

  10. Oracle常用SQL与练习

    基本 数学函数 rownum相关 分页查询 (假设每页显示10条) 不包含排序: 包含排序: 时间处理 1. to_char和to_date基本使用 eg1: eg2: 2)months_betwee ...