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. WCF分布式服务1-核心概念

    参考msdn library for WCF Windows Communication Foundation (WCF) 是用于构建面向服务的应用程序的框架. 借助 WCF,可以将数据作为异步消息从 ...

  2. keil的51项目创建

    keil的51项目创建步骤: 工程创建: Project->New uVision Project 项目命名:如...test CPU->Atmel::AT89C51 文件创建: File ...

  3. usermod语法

    语法 usermod [-LU][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数>][-g <群组>][-G ...

  4. Dubbo的Filter实战--整合Oval校验框架

    前言: 其实很早之前就想写一篇关于oval和具体服务相整合的常见做法, 并以此作为一篇笔记. 趁现在项目中间空闲期, 刚好对dubbo的filter有一些了解. 因此想结合两者, 写一下既结合校验框架 ...

  5. Dubbo透传traceId/logid的一种思路

    前言: 随着dubbo的开源, 以及成为apache顶级项目. dubbo越来越受到国内java developer欢迎, 甚至成为服务化自治的首选方案. 随着微服务的流行, 如何跟踪整个调用链, 成 ...

  6. Python学习笔记(day23更新)

    第一章 计算机基础 1.1 硬件 计算机基本的硬件由:CPU / 内存 / 主板 / 硬盘 / 网卡 / 显卡 等组成,只有硬件但硬件之间无法进行交流和通信. 1.2 操作系统 作用:操作系统用于协同 ...

  7. 初学vue----动画过渡transition简单部分

    使用动画效果要用transition包裹,transition(<trsnsition name="xx"><div></div></tr ...

  8. python:递归函数(汉诺塔)

    #hanoi.py def hanoi(n,x,y,z): if n==1: print(x,"-->",z) else: hanoi(n-1,x,z,y) print(x, ...

  9. LeetCode - Maximum Frequency Stack

    Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...

  10. Unity在WPF中的应用

    1. 本文的实现类继承于IRepository using System; using System.Linq; using System.Linq.Expressions; using Zhang. ...