lintcode : 二叉树的序列化和反序列化
题目
二叉树的序列化和反序列化
设计一个算法,并编写代码来序列化和反序列化二叉树。将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”。
如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉树序列化为一个字符串,并且可以将字符串反序列化为原来的树结构。
给出一个测试数据样例, 二叉树{3,9,20,#,#,15,7}
,表示如下的树结构:
3
/ \
9 20
/ \
15 7
我们的数据是进行BFS遍历得到的。当你测试结果wrong answer时,你可以作为输入调试你的代码。
你可以采用其他的方法进行序列化和反序列化。
解题
参考九章程序
看看注释就理解了,但是我表示自己想不出来Java
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
class Solution {
/**
* This method will be invoked first, you should design your own algorithm
* to serialize a binary tree which denote by a root node to a string which
* can be easily deserialized by your own "deserialize" method later.
*/
public String serialize(TreeNode root) {
// write your code here
if( root == null)
return "{}";
ArrayList<TreeNode> queue = new ArrayList<TreeNode>();
queue.add(root);
// 将二叉树的个节点按照从上到下、从左到有的存储在queue中
for(int i=0;i<queue.size();i++){
TreeNode q = queue.get(i);
if(q== null)
continue;
queue.add(q.left);
queue.add(q.right);
}
// 去除叶子节点的左右孩子,这个孩子是空值
while(queue.get(queue.size() - 1) == null){
queue.remove(queue.size() - 1);
}
// 遍历queue把转换成字符串
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append(queue.get(0).val);
for(int i=1;i<queue.size(); i++){
TreeNode q = queue.get(i);
if(q!= null){
sb.append(",");
sb.append(q.val);
}else{
sb.append(",#");
}
}
sb.append("}");
return sb.toString();
} /**
* This method will be invoked second, the argument data is what exactly
* you serialized at method "serialize", that means the data is not given by
* system, it's given by your own serialize method. So the format of data is
* designed by yourself, and deserialize it here as you serialize it in
* "serialize" method.
*/
public TreeNode deserialize(String data) {
// write your code here
if(data == "{}")
return null;
// 以逗号分割
String[] vals = data.substring(1,data.length()-1).split(",");
ArrayList<TreeNode> queue = new ArrayList<TreeNode>();
// 根节点
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
queue.add(root);
int index = 0;
boolean isLeftChild = true;
for (int i = 1; i < vals.length; i++) {
if (!vals[i].equals("#")) {
TreeNode node = new TreeNode(Integer.parseInt(vals[i]));
if (isLeftChild) {
queue.get(index).left = node;
} else {
queue.get(index).right = node;
}
queue.add(node);
}
if (!isLeftChild) {
index++;
}
isLeftChild = !isLeftChild;
}
return root;
}
}
lintcode : 二叉树的序列化和反序列化的更多相关文章
- [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- Leetcode 297.二叉树的序列化和反序列化
二叉树地序列化和反序列化 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据. ...
- 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化
序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...
- Java实现 LeetCode 297 二叉树的序列化与反序列化
297. 二叉树的序列化与反序列化 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得 ...
- 【LeetCode】297. 二叉树的序列化与反序列化
297. 二叉树的序列化与反序列化 知识点:二叉树:递归 题目描述 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一 ...
- [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)
描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...
- 二叉树的序列化和反序列化(Java)
请实现两个函数,分别用来序列化和反序列化二叉树 序列化就是将二叉树以字符串输出,反序列化:根据自己输出的字符串,构建二叉树. 这里先序遍历输出,且为了方便反序列化,各个节点","隔 ...
- leetcode 297二叉树的序列化与反序列化
to_string(x) 将数字x转化为string atoi(x) 将char转化为int stoi(x) 将string 转化为int 采用中序遍历的顺序存储,NULL用#表示,以,分隔,O(n) ...
- [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 ...
随机推荐
- Ubuntu 14.04下java开发环境的搭建--3--Tomcat及MySQL的安装
前面两篇文章,已经说明了JDK和Eclipse 的安装方法,下面简单说一下,Tomcat及MySQL的安装方法. Tomcat的安装. 在合适的地方解压apache-tomcat-6.0.39.tar ...
- jquery循环table中tbody的tr中input:text,将值进行拼接传入控制器并返回状态和描述
引用jquery $(function(){ $("#按钮id").click(function(){ var nums="";//变量 $("#ta ...
- 【Qt】Qt之自定义界面(窗体缩放-跨平台终极版)【转】
简述 通过上一节内容,我们实现了窗体的缩放,功能很不错,但是很遗憾-不支持跨平台!如果对于多平台来说,这是一个硬伤,所以,我们急需要一个能够支持跨平台的实现方案. 在网上看到过很多不同的实现方式,多多 ...
- 常用的php数组排序函数
分享几个php数组排序函数,每个函数出去sort是排序的意思前缀字母的含义分别代表: a 索引 k 数组键 r 逆向 u 用户自定义 顺序排序函数 sort — 对数组排序 ksort — 对数组按 ...
- 关于PHP Websocket 错误: "stream_select(): You MUST recompile PHP with a larger value of FD_SETSIZE" 的解决方案
最近在使用Ratchet (一个PHP websocket框架)改造一个PHP网站的时候,出现了错误: "It is set to 1024, but you have descriptor ...
- 用cookie实现localstorage功能
在项目中需要利用到html5的localstorage.但在利用这个属性的时候却发现无法达到预定目标.经过不断的检查及排除,最后发现原因所在: 项目中使用的浏览器是支持localstorage的,但是 ...
- C#项目连接数据库的配置
一:C# 连接SQL数据库 1.用SqlServer数据库,windows身份验证模式<add name="TestSqlSqever" providerName=&q ...
- 我的第一个MVC4程序
InputExtensions 方法解释 http://blog.csdn.net/cnceohjm/article/details/8936669 https://msdn.microsoft.co ...
- C程序调用shell脚本共有三种方法
C程序调用shell脚本共有三种法子 :system().popen().exec系列函数call_exec1.c ,内容为:system() 不用你自己去产生进程,它已经封装了,直接加入自己的命令e ...
- android----sqlite中的 query() 参数分析
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, Strin ...