LeetCode 297. Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化(C++/Java)
题目:
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
Example:
You may serialize the following tree:
1
/ \
2 3
/ \
4 5
as "[1,2,3,null,null,4,5]"
Clarification: The above format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
分析:
给定一颗二叉树将其序列化,也就是转换成一个字符串,同时还需要有一个反序列化的函数用来将字符串还原成二叉树。
采用前序遍历或者层次遍历都可以,注意的是LeetCode上要求,不要使用类的成员 / 全局 / 静态变量来存储状态,你的序列化和反序列化算法应该是无状态的。
C++中可以将 string 对象作为流处理,也就是istringstream 和 ostringstream。
程序:
C++
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
ostringstream out;
serialize(root, out);
return out.str();
}
void serialize(TreeNode* root, ostringstream& out){
if(root != nullptr){
out << root->val << ' ';
serialize(root->left, out);
serialize(root->right, out);
}
else{
out << "# ";
}
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
istringstream in(data);
return deserialize(in);
}
TreeNode* deserialize(istringstream& in){
string val;
in >> val;
if(val == "#"){
return nullptr;
}
TreeNode* root = new TreeNode(stoi(val));
root->left = deserialize(in);
root->right = deserialize(in);
return root;
}
};
Java
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root == null)
return "";
StringBuilder str = new StringBuilder();
serialize(root, str);
return str.toString();
}
public void serialize(TreeNode root, StringBuilder str){
if(root == null){
str.append("#,");
return;
}
str.append(root.val + ",");
serialize(root.left, str);
serialize(root.right, str);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data == "" || data == null)
return null;
String[] strs = data.split(",");
LinkedList<String> l = new LinkedList<>(Arrays.asList(strs));
return deserialize(l);
}
public TreeNode deserialize(LinkedList<String> list) {
if(list.getFirst().equals("#")){
list.removeFirst();
return null;
}
TreeNode root = new TreeNode(Integer.parseInt(list.getFirst()));
list.removeFirst();
root.left = deserialize(list);
root.right = deserialize(list);
return root;
}
}
LeetCode 297. Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化(C++/Java)的更多相关文章
- [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 ...
- 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化
序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...
- 297. Serialize and Deserialize Binary Tree二叉树的序列化和反序列化(就用Q)
[抄题]: Serialization is the process of converting a data structure or object into a sequence of bits ...
- [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] 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一般二叉树的编解码
由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...
- 【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 ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree
二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...
随机推荐
- 登录chatgpt的时候出现429的解决方法,亲测有效
登录chatgpt的时候出现429的解决方法 PS:在2023年3月14日晚还是可以用的,亲测有效 登录chatgpt的时候出现429的解决方法 很多时候在国内用代理进入chatgpt的时候会出现42 ...
- HarmonyOS NEXT应用开发案例—自定义日历选择器
介绍 本示例介绍通过CustomDialogController类显示自定义日历选择器. 效果图预览 使用说明 加载完成后显示主界面,点当前日期后会弹出日历选择器,选择日期后会关闭弹窗,主页面日期会变 ...
- 你的 Sleep 服务会梦到服务网格外的 bookinfo 吗
简介: ASM 产品是基于社区 Istio 定制实现的,在托管的控制面侧提供了用于支撑精细化的流量管理和安全管理的组件能力.通过托管模式,解耦了 Istio 组件与所管理的 K8s 集群的生命周期管理 ...
- Fluid 给数据弹性一双隐形的翅膀 -- 自定义弹性伸缩
简介: 弹性伸缩作为 Kubernetes 的核心能力之一,但它一直是围绕这无状态的应用负载展开.而 Fluid 提供了分布式缓存的弹性伸缩能力,可以灵活扩充和收缩数据缓存. 它基于 Runtime ...
- [GPT] 哪些职业面临 AI 威胁?
随着人工智能技术的不断发展和应用,一些重复性.机械化或标准化程度高的职业可能会面临被自动化取代的威胁.例如: 工厂生产线上的装配工人,因为许多工厂已经开始使用自动化机器人完成装配任务: 行政助理, ...
- 七天.NET 8操作SQLite入门到实战 - (2)第七天Blazor班级管理页面编写和接口对接
前言 上一章节我们引入BootstrapBlazor UI组件完成了EasySQLite后台界面的基本架子的搭建,本章节的主要内容是Blazor班级管理页面编写和接口对接. 七天.NET 8 操作 S ...
- vue3 快速入门系列 —— 状态管理 pinia
其他章节请看: vue3 快速入门 系列 pinia vue3 状态管理这里选择 pinia. 虽然 vuex4 已支持 Vue 3 的 Composition API,但是 vue3 官网推荐新的应 ...
- EPAI手绘建模APP常用工具栏_1
1.常用工具栏 图 1 常用工具栏 (1) 撤销 (2) 重做 (3) 删除 (4) 复制 ① 选中场景中的模型后,复制按钮变成可用状态,否则变成禁用状态.可以选择多个模型一起复制. (5) 变换 图 ...
- CF933-Div3 大致思路+题解
\(Rank\) A - Rudolf and the Ticket 纯水题 暴力枚举直接过 $code$ #include<bits/stdc++.h> #define fo(x,y,z ...
- 若依报错:登录状态已过期,您可以继续留在该页面,或者重新登录;When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header.
报错界面 后台报错 java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot c ...