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. WWSSN instrument response

    由于科研需要,一项任务是完成观测地震图和the short-period World-Wide Standardized Seismograph Network instrument response ...

  2. centos5&6的启动过程

    CentOS-6系统启动过程: 按下开关按钮 给服务器供电 BIOS自检操作     检查硬件是否存在异常(显示logo画面) MBR引导系统      硬盘启动系统  光驱启动系统  U盘启动系统  ...

  3. leetcode题解 200. Number of Islands(其实就是一个深搜)

    题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...

  4. 周强201771010141《面向对象程序设计(java)》第一周学习总结

    周强201771010141<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com ...

  5. 双跑道------js分机号

    博客园 首页 新随笔 联系 管理 订阅 随笔- 310  文章- 0  评论- 134  [tomcat]启动报错:Failed to initialize end point associated ...

  6. ieee trans pami latex模板

    https://www.computer.org/cms/Computer.org/transactions/templates/ https://www.computer.org/web/tpami ...

  7. CentOS6.5安装mysql5.7

    CentOS6.5安装mysql5.7 查看mysql的安装路径: [root@bogon ~]# whereis mysql mysql: /usr/bin/mysql /usr/lib/mysql ...

  8. 《Linux内核原理与分析》第八周作业

    课本:第七章 可执行程序工作原理 ELF目标文件格式 目标文件:编译器生成的文件. 目标文件的格式:out格式.COFF格式.PE(windows)格式.ELF(Linux)格式. ELF(Execu ...

  9. 开源WHMCS支付宝当面付和即时到账插件

    开源WHMCS支付宝当面付和即时到账插件 链接: https://pan.baidu.com/s/1i5HU4hn 密码: crq7

  10. 算法图解 (Aditya Bhargava 著)

    第1章 算法简介第2章 选择排序第3章 递归第4章 快速排序第5章 散列表第6章 广度优先搜索第7章 狄克斯特拉算法第8章 贪婪算法第9章 动态规划第10章 K最近邻算法第11章 接下来如何做 第1章 ...