【树】Serialize and Deserialize Binary Tree
题目:
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的更多相关文章
- [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)
描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- [LeetCode] Serialize and Deserialize Binary Tree
Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...
- LC 297 Serialize and Deserialize Binary Tree
问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...
- 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 ...
- LeetCode——Serialize and Deserialize Binary Tree
Description: Serialization is the process of converting a data structure or object into a sequence o ...
- 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 ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree
二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...
- [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 ...
随机推荐
- Vue 需要使用jsonp解决跨域时,可以使用(vue-jsonp)
1,执行命令 npm install vue-jsonp --save 2.src/main.js中添加: import VueJsonp from 'vue-jsonp' Vue.use(VueJs ...
- 配置 cxf-rs spring bean 文件
http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/docs/restful-services.html 示例: <?xm ...
- 201709013工作日记--static理解 && abstract
1.关于viewHolder设置成static的讨论 一般情况下是尽量不要使用static关键字,因为static一旦有引用变量指向了变量,使用完毕后而没有设置null,就会造成内存泄露,而且很难排查 ...
- 聚合函数、group by
聚合函数: sql语言中一种特殊的函数:聚合函数,SUM, COUNT, MAX, MIN, AVG等.这些函数和其它函数的根本区别就是它们一般作用在多条记录上.SELECT SUM(populati ...
- Android学习整理之Activity篇
一.Activity概念介绍 activity属于android的四大组件之一(其他的三个: Content provider,Broadcast receiver,Service),它可以理解为一个 ...
- 【LeetCode】 Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- jdk tomcat maven svn plsql客户端 环境变量配置整理
1 jdk 新建: 1.JAVA_HOME ----- C:\Program Files\Java\jdk1.7.0 2.CLASSPATH ------ .;%JAVA_HOME%\li ...
- 评论:一套Developer Express控件包 For Delphi7
http://www.2ccc.com/idea.asp?articleid=1675 (也可以查看盒子上这个帖子的内容) Developer Express Inc 系列控件组 for Delphi ...
- Java何时该使用覆盖?
在Java编程中,什么时候该使用覆盖函数操作呢,很多人都知道有覆盖操作,但是到底什么时候该使用覆盖操作,还是有一些模糊的感觉,以下就举例来用代码分析就明白了, 举例生活中的案例,模拟制造手机的公司: ...
- MYC编译器源码之词法分析
前文 .NET框架源码解读之MYC编译器 和 MYC编译器源码分析之程序入口 分别讲解了 SSCLI 里示例编译器的架构和程序入口,本文接着分析它的词法分析部分的代码. 词法解析的工作都由Tok类处 ...