题目:

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)的更多相关文章

  1. [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 ...

  2. 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化

    序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...

  3. 297. Serialize and Deserialize Binary Tree二叉树的序列化和反序列化(就用Q)

    [抄题]: Serialization is the process of converting a data structure or object into a sequence of bits ...

  4. [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 ...

  5. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. Leetcode 297. Serialize and Deserialize Binary Tree

    https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...

  7. [leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码

    由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...

  8. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  9. LC 297 Serialize and Deserialize Binary Tree

    问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...

  10. 【LeetCode】297. Serialize and Deserialize Binary Tree

    二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...

随机推荐

  1. Memory Error

    不久前,为了满足工作中日常的各种实验测试需求,终于按需求组装一台塔式的server T440.但是没有多久就出现些问题,以下大概是问题和现象简单描述: (1). 最开始时,没几天就出现自动重启的问题, ...

  2. DC-1渗透靶场实战速通版

    "感谢您阅读本篇博客!如果您觉得本文对您有所帮助或启发,请不吝点赞和分享给更多的朋友.您的支持是我持续创作的动力,也欢迎留言交流,让我们一起探讨技术,共同成长!谢谢!" 文章为速通 ...

  3. 阿里云张献涛:自主最强DPU神龙的秘诀

    ​简介:读懂云计算,才能看清DPU热潮. 微信公众号搜索"弹性计算百晓生",获取更多云计算知识. 如果细数最近火爆的科技概念,DPU必然位列其中. 这是英伟达一手捧红的新造富故事, ...

  4. 内含干货PPT下载|一站式数据管理DMS及最新解决方案发布

    ​简介: 今天主要给大家介绍一站式数据管理平台DMS以及解决方案的发布.议题包含企业数据管理当前的一些痛,DMS一站式数据管理平台以及其核心技术,实时数仓解决方案以及相应的应用实践. "数聚 ...

  5. 5分钟搞定Loki告警多渠道接入

    ​简介: Loki是受Prometheus启发的水平可扩展.高可用.多租户日志聚合系统.用户既可以将Loki告警直接接入SLS开放告警,也可以先将Loki接入Grafana或Alert Manager ...

  6. [FAQ] FinalCutPro 视频背景加模糊效果

    1. 时间轴右上方,找到 倒数第二个 "显示或隐藏效果浏览器",里面有一个 "模糊" 效果: 2. "模糊"效果中的 "高斯曲线& ...

  7. OSI模型之数据链路层

    一.简介 数据链路层在物理层提供服务的基础上向网络层提供服务,其最基本的服务是将源自网络层的数据可靠地传输到相邻节点的目标机网络层.其主要作用是加强物理层传输原始比特流的功能,将物理层提供的可能出错的 ...

  8. GIS中XYZ瓦片的加载流程解析与实现

    1. 什么是XYZ瓦片 XYZ瓦片是一种在线地图数据格式,常见的地图底图如Google.OpenStreetMap 等互联网的瓦片地图服务,都是XYZ瓦片,严格来说是ZXY规范的地图瓦片 ZXY规范的 ...

  9. 四、Doris物化视图

    使用场景:  在实际的业务场景中,通常存在两种场景并存的分析需求:对固定维度的聚合分析 和 对原始明细数据任意维度的分析. 例如,在销售场景中,每条订单数据包含这几个维度信息(item_id, sol ...

  10. 一、Doris演进史

    Apache Doris -- 为分析而生 Doris发展历程: Doris发展比较重要的关键节点与事件 #2008 - Doris1 :「筑巢引凤」的重要基石 早年,百度最主要的收入来源是广告.广告 ...