public class Codec
{
// Encodes a tree to a single string.
public string serialize(TreeNode root)
{
if (root == null)
return ""; Queue<TreeNode> queue = new Queue<TreeNode>();
StringBuilder result = new StringBuilder(); queue.Enqueue(root);
while (true)
{
if (queue.Count() == )
break; int nodesAtLevel = queue.Count();
StringBuilder list = new StringBuilder();
for (int i = ; i < nodesAtLevel; i++)
{
var node = queue.Dequeue();
if (node != null)
{
list.Append(node.val.ToString() + ",");
if (node.left != null)
queue.Enqueue(node.left);
else
queue.Enqueue(null); if (node.right != null)
queue.Enqueue(node.right);
else
queue.Enqueue(null);
}
else
{
list.Append("null" + ",");
} }
result.Append(list.ToString());
}
result.Remove(result.Length - , );
return result.ToString();
} // Decodes your encoded data to tree.
public TreeNode deserialize(string data)
{
if (data.Length == )
return null;
Queue<TreeNode> queue = new Queue<TreeNode>();
var tlist = data.Split(',');
var count = tlist.Length; foreach (var l in tlist)
{
if (l != "null")
{
var t = new TreeNode(int.Parse(l));
queue.Enqueue(t);
}
else
{
queue.Enqueue(null);
}
}
var root = queue.Dequeue();
var list = new List<TreeNode>();
list.Add(root);
while (queue.Any())
{
var temp = new List<TreeNode>();
for (int i = ; i < list.Count; i++)
{
var p = list[i];
var left = queue.Dequeue();
if (left != null)
{
p.left = left;
}
var right = queue.Dequeue();
if (right != null)
{
p.right = right;
}
if (p.left != null)
{
temp.Add(p.left);
}
if (p.right != null)
{
temp.Add(p.right);
}
}
list = temp;
}
return root;
}
}

leetcode297的更多相关文章

  1. [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree

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

  2. LeetCode297. Serialize and Deserialize Binary Tree

    题目 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据. 请设计一个算法来实 ...

  3. leetcode297. 二叉树的序列化与反序列化

    代码 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * ...

  4. string流

    istringstream和ostringstream 从istringstream类中读取数据赋值给某个string,写入某个string到ostringstream类,头文件<sstream ...

随机推荐

  1. Spring Boot 常见标签

    @Controller(value=“名字”,descripation="描述",tags="具体" ) @RestController控制器(path=&qu ...

  2. Python实现基于DES加密源码的文本加密器

    这是自行制作的一个DES文本加密工具 最终效果图: 本加密器支持UTF-8字符的加解密(包含中文),由于其中的编码方式与常用编码方式不同,加密结果与网上工具不同,但是能实现正常加解密. 最终目标: 目 ...

  3. I/O简介

    用户空间是常规进程所在区域.JVM就是常规进程,驻守于用户空间.用户空间是非特权区域,在该区域执行的代码不能直接访问硬件设备. 内核空间是操作系统所在区域.内核代码有特别的权利:它能与设备控制器通讯, ...

  4. 初窥async,await

    首先是一道今日头条的面试题:(听说是今日头条的并且已经烂大街了) async function async1() { console.log( 'async1 start' ) await async ...

  5. Codeforces1062C. Banh-mi(贪心+快速幂)

    题目链接:传送门 题目: C. Banh-mi time limit per test second memory limit per test megabytes input standard in ...

  6. VUE2中使用mint-ui,日期选择picker

    首先页面引入需要使用的组件 import { DatetimePicker,Toast,Popup,Picker } from 'mint-ui'; methods部分 openPicker () { ...

  7. angular 使用window事件

    1. 使用host   2. 使用HostListener 推荐使用第二种方式. 不推荐下面的方法,虽然也能进行window事件的绑定,但组件销毁后,window事件任然保留,即使手动在组件的ngOn ...

  8. PythonStudy——列表与字典推导式 List and dictionary derivation

    # 快速生成列表或字典的语法糖,且能在生成过程中添加简单的逻辑 # 能被列表推导式推导的数据源必须在循环取值时可以得到一个值 ls = [v for v in range(1, 6)] print(l ...

  9. Redis缓存系统(一)Java-Jedis操作Redis,基本操作以及 实现对象保存

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/jiangtao_st/article/details/37699473 源码下载: http://d ...

  10. 1.1.19 Word中表格自动断开

    1.修改前效果如下图所示: 2.先右键点击表格的左上角的“被正方形包着的四方箭头”, 如下图中的序号1,在出现的快捷菜单上点击[表格属性],出现[表格属性]对话框. 3.将参数设置成“允许跨页断行”, ...