leetcode297
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的更多相关文章
- [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 ...
- LeetCode297. Serialize and Deserialize Binary Tree
题目 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据. 请设计一个算法来实 ...
- leetcode297. 二叉树的序列化与反序列化
代码 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * ...
- string流
istringstream和ostringstream 从istringstream类中读取数据赋值给某个string,写入某个string到ostringstream类,头文件<sstream ...
随机推荐
- Jmeter处理返回结果的值
接口测试中,获取返回结果的值可以用插件JSON Path Extractor 此插件可以直接处理json,通过key来取值. 该插件下载地址为:http://jmeter-plugins.org/wi ...
- Linux每天一个命令:nc/ncat
nmap-ncat.x86_64版nc/ncat nc/ncat所做的就是在两台电脑之间建立链接并返回两个数据流,在这之后所能做的事就看你的想像力了.你能建立一个服务器,传输文件,与朋友聊天,传输流媒 ...
- My First Linux Module
My First Linux Module Today, I successfully build my first linux hello module. First of all add a di ...
- 团队项目(MVP------新能源无线充电管理网站)(总结)
经过了几个月的学习时间与团队的磨合以及一系列的困难之后,我们mvp小组一起完成了这个项目,内心也是十分激动和有成就感的.其实一开始基础并不好,很多都不知道,但是通过在慕课网上的学习以及老师严厉地督促下 ...
- mac出现zsh: command not found: ping解决方法
Step1:终端输入以下命令: /sbin/ping 若出现如下信息,说明包含ping命令,是zsh的 PATH有问题,表示没有加载sbin下的命令,需要编辑.zshrc文件. Step2:终端打开. ...
- 《从Lucene到Elasticsearch:全文检索实战》学习笔记五
今天我给大家讲讲tf-idf权重计算 tf-idf权重计算: tf-idf(中文词频-逆文档概率)是表示计算词项对于一个文档集或语料库中的一份文件的重要程度.词项的重要性随着它在文档中出现的次数成正比 ...
- 数组排序自定义comparator()
案例1:现在有一个普通数组arr = [3,1,2,4,5,6,8,0,1]; 自定义一个排序方法: function createComparator(){ return function (obj ...
- Angular + Websocket
Angular使用RxJS,它本质上是一个反应式扩展的javascript实现.这是一个使用可观察序列组成异步和基于事件的程序的库,非常适合使用WebSockets. 简而言之,RxJS允许我们从we ...
- 全志A33 lichee lvds屏幕配置
开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 芯灵思SinlinxA33开 ...
- Keepalived+LVS实现高可用负载均衡双主模式
LVS是一种集群(Cluster)技术:采用IP负载均衡技术和基于内容请求分发技术.调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行,且调度器自动屏蔽掉服务器的故障,从而将一组服务器构成一 ...