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 ...
随机推荐
- hdu 1879 继续畅通project
本题链接:pid=1879http://">点击打开链接 本题大意: 输入n行数据.每行数据前两个表示该条路连通的两个村庄的编号,第三个表示修该条路的成本.最后的0或1表示该路未修或已 ...
- Hashmap在JDK8中的提升
HashMap使用key的hashCode()和equals()方法来将值划分到不同的桶里. 桶的数量通常要比map中的记录的数量要稍大.这样 每一个桶包含的值会比較少(最好是一个).当通过key进行 ...
- 【IOS】启动画面
总述: 两种方式,一种是使用系统自带的.按规则定义启动图片名称就可以,显示为1秒,要想延长时间,用[nsthread sleepForTimeInterval:5.0] ,还有一种就是自己定义ui ...
- Docker vs. Kubernetes vs. Apache Mesos: Why What You Think You Know is Probably Wrong
Docker vs. Kubernetes vs. Apache Mesos: Why What You Think You Know is Probably Wrong - Mesosphere h ...
- (5)在tomcat运行自己的javaweb项目
A:在MyEclipse下方的Servers栏中启动服务器,运行项目: 1,选中项目所在的tomcat服务器 2,点击“启动按钮”,见下图 3,启动以后,看控制台输出日志: B:从服务器按钮启动: 1 ...
- 原生js写简单轮播图方式1-从左向右滑动
轮播图就是让图片每隔几秒自动滑动,达到图片轮流播放的效果.轮播图从效果来说有滑动式的也有渐入式的,滑动式的轮播图就是图片从左向右滑入的效果,渐入式的轮播图就是图片根据透明度渐渐显示的效果,这里说的是实 ...
- Ubuntu下Jupyter Notebook的安装
pip install --upgrade pip //更新pip pip install jupyter sudo apt install jupyter-notebook 运行 jupyter-n ...
- 使用Kotlin如何startActivity
没错,就是这么简单的一个功能,不过由于初学kotlin,所以找了很久才找到如何写,所以还是贴出来给需要的人吧,上代码: startActivity(Intent(MainActivity@this, ...
- python dig trace 功能实现——通过Querying name server IP来判定是否为dns tunnel
dns tunnel确认方法,查询子域名最终的解析地址: 使用方法:python dig_trace.py "<7cf1e56b 67fc90f8 caaae86e 0787e907 ...
- SELinux 初探
SELinux:Security Enhanced Linux.SELinux 是 NSA(美国国家安全局)开发设计,整合到 Linux 内核中的一个模块. 0. 基本概念 DAC(Discretio ...