449 Serialize and Deserialize BST 序列化和反序列化二叉搜索树
详见: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 序列化和反序列化二叉搜索树的更多相关文章
- Java实现 LeetCode 449 序列化和反序列化二叉搜索树
449. 序列化和反序列化二叉搜索树 序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法 ...
- 【leetcode-449】序列化和反序列化二叉搜索树
序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法来序列化和反序列化二叉搜索树. 对序列 ...
- [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [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 ...
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- 449. Serialize and Deserialize BST
https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...
- 【leetcode】449. Serialize and Deserialize BST
题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...
- 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 ...
- LeetCode 449. Serialize and Deserialize BST
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
随机推荐
- Storm专题二:Storm Trident API 使用具体解释
一.概述 Storm Trident中的核心数据模型就是"Stream",也就是说,Storm Trident处理的是Stream.可是实际上Stream是被成批处理的. ...
- poj1664 dp记忆化搜索
http://poj.org/problem?id=1664 Description 把M个相同的苹果放在N个相同的盘子里,同意有的盘子空着不放,问共同拥有多少种不同的分法?(用K表示)5.1.1和1 ...
- EXTJS 4 树形表格组件使用演示样例
EXTJS 4 树形表格组件使用演示样例 一.总体效果图 version=1&modificationDate=1412058826000&api=v2" alt=" ...
- web 开发之js---ajax 异步处理
本文介绍了如何创建能够适应不同浏览器的XMLHttpRequest实例,建立和发送请求,并响应服务器.您将开始接触最基本和基础性的有关Ajax的全部对象和编程方法:XMLHttpRequest对象.该 ...
- Cocos2d-x 3.2 Lua演示样例CurrentLanguageTest(当前语言环境)
Cocos2d-x 3.2 Lua演示样例CurrentLanguageTest(当前语言环境) 转载请注明:IT_xiao小巫 本篇博客介绍Cocos2d-x 3.2给我们提供的一个样例.获取当前程 ...
- 在windows cgywinportable上,通过运行linux命令,批量改动文件名。
在windows cgywinportable上.通过运行linux命令.批量改动文件名. 实例:将当前文件夹下的全部文件名称加上.sql find ./ -type f -exec mv {} ' ...
- 理解static关键字
1.static 变量是类变量,通过类名引用. 2.static 方法不需要针对某个对象进行操作,其运行结果仅仅与输入的参数有关,调用时直接类名引用. 3.static 方法不能对非静态成员进行访问. ...
- 电商系统的演变可以看出架构演变 Dubbo入门 远程过程调用 需要解决的问题
Dubbo入门---搭建一个最简单的Demo框架 - CSDN博客 https://blog.csdn.net/noaman_wgs/article/details/70214612 Dubbo背景和 ...
- XMU 1613 刘备闯三国之三顾茅庐(一) 【并查集】
1613: 刘备闯三国之三顾茅庐(一) Time Limit: 1000 MS Memory Limit: 128 MBSubmit: 99 Solved: 29[Submit][Status][ ...
- publish and submit
http://blog.csdn.net/w_jewelry/article/details/8123639 1.Gerrit里点击“publish and submit”提示如下:Your chan ...