[leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码
由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下:
记录二叉树的结构信息,也就是空节点用符号表示
一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来
解码的时候可以按照前序的顺序依次还原节点。
/*
前序遍历或者层序遍历都可以,前序遍历要保存二叉树的结构信息
空节点用符号表示
*/
StringBuilder s = new StringBuilder();
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
preTra(root);
return new String(s);
}
public void preTra(TreeNode root)
{
if (root==null)
{
//#代表空节点
s.append("#");
s.append(",");
return;
}
s.append(root.val);
s.append(",");
preTra(root.left);
preTra(root.right);
} // Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.length()==0) return null;
String[] d = data.split(",");
//用一个链表记录节点,在调用函数中对链表进行改变是会改变堆中真正的链表的
LinkedList<String> list = new LinkedList<>();
for (String s :
d) {
list.add(s);
}
return dehelper(list);
}
public TreeNode dehelper(LinkedList<String> list)
{
//pollfirst和poll的区别是,前者在为空时会返回null
String s = list.pollFirst();
if (s==null||s.equals("#")) return null;
//按照前序遍历的顺序,依次把节点放回去
TreeNode cur = new TreeNode(Integer.parseInt(s));
cur.left = dehelper(list);
//注意下边这list和上边的list已经不一样了,因为在上边函数中改变了
// 虽然传入的是list变量的副本,但是原本和副本都是指向一个地址,都会造成改变
cur.right = dehelper(list);
return cur;
}
[leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码的更多相关文章
- [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 ...
- Leetcode 297. Serialize and Deserialize Binary Tree
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...
- [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 ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- LC 297 Serialize and Deserialize Binary Tree
问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...
- [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)
描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...
- 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' 和 输出流 ...
- 297. Serialize and Deserialize Binary Tree
题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...
随机推荐
- bootstrap 按钮颜色属性
bootstrap 按钮颜色属性有几种
- python 子进程
1.线程的介绍(理论部分) 1.1 进程线程分工 我们之前讲运行一个py文件,就是开启了一个进程,在内存中开辟一个进程空间,将必要的数据加载到这个进程空间,然后cpu在去调用这个进程的主线程去执行具体 ...
- OpenCV击中击不中HMTxingt变换最容易理解的解释
OpenCV击中击不中变换是几个形态变换中相对比较拗口.不容易理解的,给初学者理解带来了很多困难,虽然网上也有许多的公开资料,原理和算法基本上介绍比较清晰,但是是要OpenCV进行形态变换大多还是说得 ...
- moviepy音视频剪辑:视频剪辑基类VideoClip详解
☞ ░ 前往老猿Python博文目录 ░ 一.概述 在<moviepy音视频剪辑:moviepy中的剪辑基类Clip详解>和<moviepy音视频剪辑:moviepy中的剪辑基类Cl ...
- PyQt(Python+Qt)实现的GUI图形界面应用程序的事件捕获方法大全及对比分析
一. 概述 PyQt的图形界面应用中,事件处理类似于Windows系统的消息处理.一个带图形界面的应用程序启动后,事件处理就是应用的主循环,事件处理负责接收事件.分发事件.接收应用处理事件的返回结果, ...
- 威联通(NAS)搭建个人音乐中心
我为什么要自己搭建音乐服务 曾记得早些年,音乐是可以随便在线听,随便下载的,没有付费这么一说的(背后是音乐平台提供的版权支持).我们听音乐也就可以很随意,但是这几年,音乐的版权开始管理的严禁,音乐没地 ...
- 【译】为什么Rust中的BTreeMap没有with_capacity()方法?
原文标题:Why doesn't Rust's BTreeMap have a with_capacity() method? 原文链接:https://www.nicolas-hahn.com/20 ...
- (干货)构建镜像之docker commit
Docker提供了两种构建镜像的方法:docker commit命令喝Dockerfile构建文件. docker commit 不推荐 (1).这是手工构建镜像的方式,容易出错,效率低且可重复性 ...
- 四、testNG.xml 简单介绍
TestNG定义了一套非常具体的术语描述测试. testng.xml testng.xml是一个以XML记录所有测试的文件.可以利用这个文件,跑同一个类或者多个不同类里面的测试用例. testng.x ...
- AOP基本概念
连接点joinpoint(类中所有方法) 切入点pointcut(缺少共性代码的方法) 通知advice(被抽取的共性功能的代码逻辑,通知有位置区分,也就是从切入点方法中被抽取代码的前面还是后面抽象出 ...