详见:https://leetcode.com/problems/serialize-and-deserialize-bst/description/

C++:

/**
* 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 os;
serializeHelper(root,os);
return os.str();
} // Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
istringstream is(data);
return deserializeHelper(is);
}
void serializeHelper(TreeNode* root,ostringstream &os)
{
if(!root)
{
os<<"# ";
}
else
{
os<<root->val<<" ";
serializeHelper(root->left,os);
serializeHelper(root->right,os);
}
}
TreeNode* deserializeHelper(istringstream &is)
{
string val;
is>>val;
if(val=="#")
{
return nullptr;
}
TreeNode* node=new TreeNode(stoi(val));
node->left=deserializeHelper(is);
node->right=deserializeHelper(is);
return node;
}
}; // Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));

参考:https://www.cnblogs.com/grandyang/p/6224510.html

449 Serialize and Deserialize BST 序列化和反序列化二叉搜索树的更多相关文章

  1. Java实现 LeetCode 449 序列化和反序列化二叉搜索树

    449. 序列化和反序列化二叉搜索树 序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法 ...

  2. 【leetcode-449】序列化和反序列化二叉搜索树

    序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法来序列化和反序列化二叉搜索树. 对序列 ...

  3. [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)

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

  4. [leetcode]449. Serialize and Deserialize BST序列化与反序列化BST

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

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

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

  6. 449. Serialize and Deserialize BST

    https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...

  7. 【leetcode】449. Serialize and Deserialize BST

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

  8. 449. Serialize and Deserialize BST——几乎所有树的面试题目都会回到BFS或者DFS,使用BFS,None节点存#

    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

    原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...

随机推荐

  1. redux-saga 异步流

    前言 React的作用View层次的前端框架,自然少不了很多中间件(Redux Middleware)做数据处理, 而redux-saga就是其中之一,目前这个中间件在网上的资料还是比较少,估计应用的 ...

  2. android JNI 资料大全

    AndroidJNI 通过C++调用JAVA 1. JNIEnv对象    对于本地函数    JNIEXPORT void JNICALL Java_video1_TestNative_sayHel ...

  3. 【Mongodb教程 第十六课 】 分享NO-SQL开发实战

    最近研究了一下NOSQL,现整理目录如下: 一.关系数据库的瓶颈: 二.NOSQL概述: 三.NOSQL中的热门数据库MongoDB介绍及安装配置: 四.MongoDB开发模式及实战: 一.关系数据库 ...

  4. 【Mongodb教程 第七课 】MongoDB 查询文档

    find() 方法 要从MongoDB 查询集合数据,需要使用MongoDB 的 find() 方法. 语法 基本的find()方法语法如下 >db.COLLECTION_NAME.find() ...

  5. Apache Hadoop 和Hadoop生态圈

    Apache Hadoop 和Hadoop生态圈 Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户能够在不了解分布式底层细节的情况下.开发分布式程序.充分利用集群的威力进行快速 ...

  6. jquery源码学习笔记一:总体结构

    练武不练功,到老一场空.计算机也一样. 计算机的功,就是原理.如果程序员只会使用各种函数,各种框架,而不知其原理,顶多熟练工人而已.知其然,更要知其所以然. jquery我们用得很爽,但它究竟咋实现的 ...

  7. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) E. Prairie Partition 二分+贪心

    E. Prairie Partition It can be shown that any positive integer x can be uniquely represented as x =  ...

  8. mysql10---索引优化

    D:\MYSQL\mysql-winx64\data\WIN-20171216YUR-slow.log是慢日志: ; ; # Time: :.472000Z # # Query_time: Rows_ ...

  9. Ubuntu 12.10安装vmware-tools

    1:[菜单]->[虚拟机]->[重新安装vmware tools]出现 图中下边说的很清楚,解压然后执行 2:把压缩包拷贝到 /home/下,然后执行 :tar -zxvf v[按住tab ...

  10. qemu-kvm磁盘读写的缓冲(cache)的五种模式

    qemu-kvm磁盘读写的缓冲(cache)模式一共有五种,分别是writethrough, wirteback, none, unsafe, directsync当你对VM读写磁盘的性能有不同的要求 ...