LintCode-Serialization and Deserialization Of Binary Tree
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'.
There is no limit of how you deserialize or serialize a binary tree, you only need to make sure you can serialize a binary tree to a string and deserialize this string to the original structure.
An example of testdata: Binary tree {3,9,20,#,#,15,7}, denote the following structure:
3
/ \
9 20
/ \
15 7
Our data serialization use bfs traversal. This is just for when you got wrong answer and want to debug the input.
You can use other method to do serializaiton and deserialization.
Solution:
/**
* 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) {
String res = "";
if (root==null) return res; List<TreeNode> nodeList = new ArrayList<TreeNode>();
nodeList.add(root);
int index = 0;
res = Integer.toString(nodeList.get(0).val);
while (index<nodeList.size()){
TreeNode cur = nodeList.get(index);
if (cur==null){
res += ",#,#";
} else {
if (cur.left!=null){
nodeList.add(cur.left);
res += ","+cur.left.val;
} else res += ",#";
if (cur.right!=null){
nodeList.add(cur.right);
res += ","+cur.right.val;
} else res += ",#";
}
index++;
} return res;
} /**
* 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) {
if (data.isEmpty()) return null;
String[] temp = data.split(",");
TreeNode root = new TreeNode(Integer.parseInt(temp[0]));
List<TreeNode> nodeList = new ArrayList<TreeNode>();
nodeList.add(root);
int cur = 0, next = 1;
while (cur<nodeList.size()){
TreeNode curNode = nodeList.get(cur);
if (curNode==null) cur++;
else {
int left = next;
int right = next+1;
if (next<temp.length){
if (temp[next].equals("#")) nodeList.add(null);
else {
TreeNode leftChild = new TreeNode(Integer.parseInt(temp[next]));
curNode.left = leftChild;
nodeList.add(leftChild);
}
}
if (next+1<temp.length){
if (temp[next+1].equals("#")) nodeList.add(null);
else {
TreeNode rightChild = new TreeNode(Integer.parseInt(temp[next+1]));
curNode.right = rightChild;
nodeList.add(rightChild);
}
}
next += 2;
cur++;
}
} return root;
}
}
LintCode-Serialization and Deserialization Of Binary Tree的更多相关文章
- Lintcode: Binary Tree Serialization (Serialization and Deserialization Of Binary Tree)
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...
- LintCode 7.Serialize and Deserialize Binary Tree(含测试代码)
题目描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将 ...
- [LintCode] Binary Tree Serialization
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- 【LeetCode】Verify Preorder Serialization of a Binary Tree(331)
1. Description One way to serialize a binary tree is to use pre-order traversal. When we encounter a ...
- LeetCode Verify Preorder Serialization of a Binary Tree
原题链接在这里:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ 题目: One way to ...
- LeetCode OJ 331. Verify Preorder Serialization of a Binary Tree
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...
- Verify Preorder Serialization of a Binary Tree
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...
- 331. Verify Preorder Serialization of a Binary Tree
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...
随机推荐
- leetcode 1
题目: 最开始采用暴力解法,两个for循环遍历所有组合形式,时间复杂度为O(n2),代码省略. 进一步学习,采用hash表存储,空间换时间,时间复杂度为O(n),空间复杂度为O(n); 将数组放入ha ...
- Android IOS WebRTC 音视频开发总结(四五)-- ORTC背后的真相
本文主要介绍ORTC(Object Real-time Communication),支持原创,文章来自博客园RTC.Blacker,支持原创,转载必须说明出处,更多详见www.rtc.help. - ...
- Android IOS WebRTC 音视频开发总结(三二)-- WebRTC项目开发建议
本文主要介绍WEBRTC开发过程中的一些现象,文章来自博客园RTC.Blacker,支持原创,欢迎关注微信公众号blacker,更多详见www.rtc.help 随着移动互联网和智能硬件的快速发展,音 ...
- gdb调试工具使用方法分享
刚才看了一个CSDN上分享gdb调试工具使用的教程,讲得非常好,推荐到这里: http://blog.csdn.net/liigo/article/details/582231
- SAP物料价格评估与成本计算体系
物料评估目的 核算物料领用与消耗的价值,使生产成本与产品销售成本有统一计价标准. 核算企业库存价值. 物料价格方法 移动平均价,标准价,实际价,计划价. @移动平均价: 每次物料移动后重新计算平均价, ...
- 很不错的安卓FadingActionBar控件源码
这个我刚刚从一个github下载过来的,自己测试一下运行了,感觉还不错的,安卓FadingActionBar控件源码,现在就给大家分享一下,源码我已经上传到源码天堂那里了,想下载的朋友可以去那里下载吧 ...
- 音乐社交APP源码 V1.1
1.关于音乐曲库,对接的是百度音乐,会自动随搜索链接百度曲库2.便捷聊天,采用xmpp基本架构.3.加入和整理了群聊天.4.分布式聊天,喜欢该专辑直接进入聊天,喜欢该音乐的进入聊天.5.采用兴趣社交和 ...
- Ubuntu系统使用记录(持续更新)
本篇文章记录在虚拟机上跑Ubuntu16.04遇到的一系列问题,熟悉一下Ubuntu的相关操作,进入终端的方法ctrl+alt+t. 1.修改屏幕分辨率,进入系统默认的是800x600 即便能够进入s ...
- atexit注册的函数是在main函数之后执行?
跟atexit函数相识已久,man手册里对atexit的解释是这么一段: The atexit() function registers the given function to be called ...
- Hadoop在win7下部署的问题
问题: 为了测试方便所以在win7下部署了伪分布式hadoop运行环境,但是部署结束后在命令行运行hadoop命令创建一个用户文件目录时出现了一下情况: 系统找不到指定的批标签- make_comma ...