题目:

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
/ \
2 3
/ \
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.

思路:

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

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/ /**
* Encodes a tree to a single string.
*
* @param {TreeNode} root
* @return {string}
*/
var serialize = function(root) {
if(root==null){
return [];
} var queue=[],res=[];
queue.push(root); while(queue.length!=0){
var p=queue.unshift();
res.push(p.val);
if(p!=null){
queue.push(p.left.val);
queue.push(p.right.val);
}
} return res;
}; /**
* Decodes your encoded data to tree.
*
* @param {string} data
* @return {TreeNode}
*/
var deserialize = function(data) {
if(data.length==0){
return null;
}
var root=new TreeNode(data[0]),queue=[],res,loc=1;
queue.push(root);
while(queue.length!=0){
var p=queue.unshift();
var count=0;
while(count<2){
count++;
var child=null;
if(data[loc]==null){
if(loc%2!=0){
child=new TreeNode(data[loc]);
p.left=child;
}else{
child=new TreeNode(data[loc]);
p.right=child;
}
queue.push(child);
}else{
if(loc%2!=0){
p.left=null;
}else{
child=new TreeNode(data[loc]);
p.right=null;
}
}
}
} return root;
}; /**
* Your functions will be called as such:
* deserialize(serialize(root));
*/

【树】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. where /group by/ having/ order by/

    1.order by 是 按字段 进行排序.. 字段后面可跟 desc 降序..asc 升序..默认为升序2.group by 是进行分组 查询3.having 和 where 都属于 条件过滤 区别 ...

  2. Yarn application has already exited with state FINISHED

    如果在运行spark-sql时遇到如下这样的错误,可能是因为yarn-site.xml中的配置项yarn.nodemanager.vmem-pmem-ratio值偏小,它的默认值为2.1,可以尝试改大 ...

  3. Wireshark数据包分析(一)——使用入门

    Wireshark简介: Wireshark是一款最流行和强大的开源数据包抓包与分析工具,没有之一.在SecTools安全社区里颇受欢迎,曾一度超越Metasploit.Nessus.Aircrack ...

  4. Maven发布和管理项目

    1 什么是Maven? 如果没有Maven,你可能不得不经历下面的过程: 1 如果使用了spring,去spring的官网下载jar包:如果使用hibernate,去hibernate的官网下载Jar ...

  5. 查看sql server数据库连接数的三种方法

    怎样才能查看sql server数据库连接数呢?下面就将为您介绍三种查看的方法,供您参考,希望能够帮助到您. 1.通过系统的“性能”来查看:开始->管理工具->性能(或者是运行里面输入 m ...

  6. C#在dataGridView中遍历,寻找相同的数据并定位

      1. C#在dataGridView中遍历,寻找相同的数据并定位   [c-sharp] view plain copy int row = dataGridView1.Rows.Count;// ...

  7. 实现单台测试机6万websocket长连接

    本文由作者郑银燕授权网易云社区发布. 本文是我在测试过程中的记录,实现了单台测试机发起最大的websocket长连接数.在一台测试机上,连接到一个远程服务时的本地端口是有限的.根据TCP/IP协议,由 ...

  8. Linux openvswitch 性能调优-flow-eviction-threshold

    原文:https://www.cnblogs.com/scottieyuyang/p/5683656.html Increasing the flow-eviction threshold The t ...

  9. OpenSL的使用

    #include <jni.h> #include <string> #include <SLES/OpenSLES.h> #include <SLES/Op ...

  10. Flask从入门到精通之MySQL数据库操作

    前面的章节中我们已经学习了如何建立模型和关系,接下来我们学习如何使用模型的最好方法是在Python shell 中实际操作.并将介绍最常用的数据库操作. 一.创建表 首先,我们要让Flask-SQLA ...