题目:

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

For example, you may serialize the following tree

  1. 1
  2. / \
  3. 2 3
  4. / \
  5. 4 5

as "[1,2,3,null,null,4,5]", just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

思路:

序列化:层序遍历二叉树。 
反序列化:层序构建二叉树。

  1. /**
  2. * Definition for a binary tree node.
  3. * function TreeNode(val) {
  4. * this.val = val;
  5. * this.left = this.right = null;
  6. * }
  7. */
  8.  
  9. /**
  10. * Encodes a tree to a single string.
  11. *
  12. * @param {TreeNode} root
  13. * @return {string}
  14. */
  15. var serialize = function(root) {
  16. if(root==null){
  17. return [];
  18. }
  19.  
  20. var queue=[],res=[];
  21. queue.push(root);
  22.  
  23. while(queue.length!=0){
  24. var p=queue.unshift();
  25. res.push(p.val);
  26. if(p!=null){
  27. queue.push(p.left.val);
  28. queue.push(p.right.val);
  29. }
  30. }
  31.  
  32. return res;
  33. };
  34.  
  35. /**
  36. * Decodes your encoded data to tree.
  37. *
  38. * @param {string} data
  39. * @return {TreeNode}
  40. */
  41. var deserialize = function(data) {
  42. if(data.length==0){
  43. return null;
  44. }
  45. var root=new TreeNode(data[0]),queue=[],res,loc=1;
  46. queue.push(root);
  47. while(queue.length!=0){
  48. var p=queue.unshift();
  49. var count=0;
  50. while(count<2){
  51. count++;
  52. var child=null;
  53. if(data[loc]==null){
  54. if(loc%2!=0){
  55. child=new TreeNode(data[loc]);
  56. p.left=child;
  57. }else{
  58. child=new TreeNode(data[loc]);
  59. p.right=child;
  60. }
  61. queue.push(child);
  62. }else{
  63. if(loc%2!=0){
  64. p.left=null;
  65. }else{
  66. child=new TreeNode(data[loc]);
  67. p.right=null;
  68. }
  69. }
  70. }
  71. }
  72.  
  73. return root;
  74. };
  75.  
  76. /**
  77. * Your functions will be called as such:
  78. * deserialize(serialize(root));
  79. */

【树】Serialize and Deserialize Binary Tree的更多相关文章

  1. [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)

    描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...

  2. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  3. [LeetCode] Serialize and Deserialize Binary Tree

    Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...

  4. LC 297 Serialize and Deserialize Binary Tree

    问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...

  5. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. LeetCode——Serialize and Deserialize Binary Tree

    Description: Serialization is the process of converting a data structure or object into a sequence o ...

  7. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  8. 【LeetCode】297. Serialize and Deserialize Binary Tree

    二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...

  9. [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 ...

随机推荐

  1. Vue 需要使用jsonp解决跨域时,可以使用(vue-jsonp)

    1,执行命令 npm install vue-jsonp --save 2.src/main.js中添加: import VueJsonp from 'vue-jsonp' Vue.use(VueJs ...

  2. 配置 cxf-rs spring bean 文件

    http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/docs/restful-services.html 示例: <?xm ...

  3. 201709013工作日记--static理解 && abstract

    1.关于viewHolder设置成static的讨论 一般情况下是尽量不要使用static关键字,因为static一旦有引用变量指向了变量,使用完毕后而没有设置null,就会造成内存泄露,而且很难排查 ...

  4. 聚合函数、group by

    聚合函数: sql语言中一种特殊的函数:聚合函数,SUM, COUNT, MAX, MIN, AVG等.这些函数和其它函数的根本区别就是它们一般作用在多条记录上.SELECT SUM(populati ...

  5. Android学习整理之Activity篇

    一.Activity概念介绍 activity属于android的四大组件之一(其他的三个: Content provider,Broadcast receiver,Service),它可以理解为一个 ...

  6. 【LeetCode】 Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  7. jdk tomcat maven svn plsql客户端 环境变量配置整理

    1 jdk 新建: 1.JAVA_HOME   -----  C:\Program Files\Java\jdk1.7.0 2.CLASSPATH  ------   .;%JAVA_HOME%\li ...

  8. 评论:一套Developer Express控件包 For Delphi7

    http://www.2ccc.com/idea.asp?articleid=1675 (也可以查看盒子上这个帖子的内容) Developer Express Inc 系列控件组 for Delphi ...

  9. Java何时该使用覆盖?

    在Java编程中,什么时候该使用覆盖函数操作呢,很多人都知道有覆盖操作,但是到底什么时候该使用覆盖操作,还是有一些模糊的感觉,以下就举例来用代码分析就明白了, 举例生活中的案例,模拟制造手机的公司: ...

  10. MYC编译器源码之词法分析

    前文  .NET框架源码解读之MYC编译器 和 MYC编译器源码分析之程序入口 分别讲解了 SSCLI 里示例编译器的架构和程序入口,本文接着分析它的词法分析部分的代码. 词法解析的工作都由Tok类处 ...