博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lintcode: Binary Tree Serialization (Serialization and Deserialization Of Binary Tree)
阅读量:6943 次
发布时间:2019-06-27

本文共 3984 字,大约阅读时间需要 13 分钟。

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.ExampleAn example of testdata: Binary tree {
3,9,20,#,#,15,7}, denote the following structure: 3 / \ 9 20 / \ 15 7Our 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.

Serialization 和 Deserialization都是用BFS, Serialization注意要删除String末尾多余的“#”, Deserialization维护一个count指示当前TreeNode对应的值

1 class Solution { 2     /** 3      * This method will be invoked first, you should design your own algorithm  4      * to serialize a binary tree which denote by a root node to a string which 5      * can be easily deserialized by your own "deserialize" method later. 6      */ 7     public String serialize(TreeNode root) { 8         // write your code here 9         StringBuffer res = new StringBuffer();10         if (root == null) return res.toString();11         LinkedList
queue = new LinkedList
();12 queue.offer(root);13 res.append(root.val);14 while (!queue.isEmpty()) {15 TreeNode cur = queue.poll();16 if (cur.left != null) queue.offer(cur.left); //add children to the queue17 if (cur.right != null) queue.offer(cur.right);18 res.append(",");19 if (cur.left != null) {20 res.append(cur.left.val);21 }22 else res.append("#");23 res.append(",");24 if (cur.right != null) {25 res.append(cur.right.val);26 }27 else res.append("#");28 }29 int i = res.length()-1;30 while (i>=0 && res.charAt(i)=='#') {31 res.deleteCharAt(i);32 res.deleteCharAt(i-1);33 i -= 2;34 }35 return res.toString();36 }37 38 /**39 * This method will be invoked second, the argument data is what exactly40 * you serialized at method "serialize", that means the data is not given by41 * system, it's given by your own serialize method. So the format of data is42 * designed by yourself, and deserialize it here as you serialize it in 43 * "serialize" method.44 */45 public TreeNode deserialize(String data) {46 // write your code here47 if (data==null || data.length()==0) return null;48 String[] arr = data.split(",");49 int len = arr.length;50 int count = 0;51 TreeNode root = new TreeNode(Integer.parseInt(arr[0]));52 LinkedList
queue = new LinkedList
();53 queue.offer(root);54 count++;55 while (!queue.isEmpty()) {56 TreeNode cur = queue.poll();57 String left="", right="";58 if (count < len) {59 left = arr[count];60 count++;61 if (!left.equals("#")) {62 cur.left = new TreeNode(Integer.parseInt(left));63 queue.offer(cur.left);64 }65 else cur.left = null;66 }67 else cur.left = null;68 69 if (count < len) {70 right = arr[count];71 count++;72 if (!right.equals("#")) {73 cur.right = new TreeNode(Integer.parseInt(right));74 queue.offer(cur.right);75 }76 else cur.right = null;77 }78 else cur.right = null;79 }80 return root;81 }82 }

 

转载地址:http://bsanl.baihongyu.com/

你可能感兴趣的文章
HTML5 一些有用的 APIs
查看>>
Unity获取指定资源目录下的所有文件
查看>>
C - The C Answer (2nd Edition) - Exercise 1-12
查看>>
linux定时任务cron 安装配置
查看>>
SequenceFile文件
查看>>
人脸和性别识别(基于OpenCV)
查看>>
Nginx Java 日志切割脚本
查看>>
浅谈代码审计入门实战:某博客系统最新版审计之旅
查看>>
nyoj 119士兵杀敌(三)(线段树区间最值查询,RMQ算法)
查看>>
truncate/drop表非常慢,怎么办?用硬链接,极速体验
查看>>
spring boot测试
查看>>
Timer使用
查看>>
H5+混合移动app应用开发——坑我太甚
查看>>
nc/netcat命令
查看>>
web3.js编译Solidity,发布,调用全部流程(手把手教程)
查看>>
Java国际化号码验证方法,国内手机号正则表达式
查看>>
HDU 1158 Employment Planning
查看>>
表格内嵌编辑控件
查看>>
DNS解析原理和流程
查看>>
Windows程序设计_15_求书
查看>>