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

如果使用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. Leetcode--easy系列10

    #205 Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are ...

  2. JS地区四级级联

    <script type="text/javascript" src="../js/jsAddress.js"></script> &l ...

  3. mysql + Fluently NHibernate + WebAPI + Autofac

    MySQL.Fluently NHibernate.WebAPI.Autofac,对我来说每一个都是麻烦疙瘩,现在它们为了一个共同的项目而凑合到一起了.一路磕磕碰碰,现在貌似有了一点眉目. 作为一个步 ...

  4. flask的路由配置,特殊装饰器

    1,flask中的路由 endpoint-url_for反向地址 endpoint默认是视图函数名endpoint="雪雪" methods 指定视图函数的请求方式,默认GET d ...

  5. 判断一个包是否可以安装是一个NP-complete问题

    1 checking whether a single package P can be installed, given a repository R,is NP-complete

  6. CXF拦截器(Interceptor)LoggingInInterceptor

    Interceptor是CXF架构中一个重要的功能.你可以在不对核心模块进行修改的情况下,动态添加很多功能(你可以想象Struts2拦截器的优点).这对于CXF这个以处理消息为中心的服务框架来说是非常 ...

  7. log4j_自定义样式参数意义

    #自定义样式 %c 输出所属的类目,通常就是所在类的全名 %C 输出Logger所在类的名称,通常就是所在类的全名 %d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比 ...

  8. mongodb09----replicattion set--健壮性

    replication set复制集 replicattion set 多台服务器维护相同的数据副本,提高服务器的可用性.一台是服务器出问题了另外2台还可以接收干,secondary平时保持只读状态, ...

  9. Lightoj 1017 - Brush (III)

    1017 - Brush (III)    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Sam ...

  10. YTU 2946: 填空:间接基类就是A

    2946: 填空:间接基类就是A 时间限制: 1 Sec  内存限制: 128 MB 提交: 132  解决: 96 题目描述 如下程序所示,D继承自B和C,而B和C均继承自A.根据继承的机制,D的对 ...