【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
标签: LeetCode
题目地址:https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/
题目描述:
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.
For example, you may serialize the following tree
1
/ \
2 3
/ \
4 5
as “[1,2,3,null,null,4,5]”, just the same as how LeetCode OJ 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.
题目大意
序列化,解序列化一棵二叉树。
解题方法
和449. Serialize and Deserialize BST多么的像呀!之前我说,只知道前序遍历是没法确定一个树的,我说的不严谨。如果前序遍历的过程中记录下哪些位置是空节点的话,就是可以确定这棵树的。LeetCode的官方树的构建就是这样的。
因此,我们采用和上题同样的方法,只不过需要把空节点记录下来。然后在反序列化时把它再变成空节点即可。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Codec:
def serialize(self, root):
"""Encodes a tree to a single string.
:type root: TreeNode
:rtype: str
"""
vals = []
def preOrder(root):
if not root:
vals.append('#')
else:
vals.append(str(root.val))
preOrder(root.left)
preOrder(root.right)
preOrder(root)
return ' '.join(vals)
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
vals = collections.deque(val for val in data.split())
def build():
if vals:
val = vals.popleft()
if val == '#':
return None
root = TreeNode(int(val))
root.left = build()
root.right = build()
return root
return build()
# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))
日期
2018 年 3 月 15 日 –雾霾消散,春光明媚
【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)的更多相关文章
- [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一般二叉树的编解码
由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...
- LC 297 Serialize and Deserialize Binary Tree
问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...
- 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 ...
- 297. Serialize and Deserialize Binary Tree *HARD*
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- kafka的安装及使用
前言花絮 今天听了kafka开发成员之一的饶军老师的讲座,讲述了kafka的前生今世.干货的东西倒是没那么容易整理出来,还得刷一遍视频整理,不过两个比较八卦的问题,倒是很容易记住了. Q:为什么kaf ...
- Oracle-distinct()用法、count(distinct( 字段A || 字段B))是什么意思?distinct多个字段
0.distinct用法 在oracle中distinct的使用主要是在查询中去除重复出现的数据 直接在字段前加distinct关键字即可,如:select distinct 名字 from tabl ...
- acquire
An acquired taste is an appreciation for something unlikely to be enjoyed by a person who has not ha ...
- [Emlog主题] Monkey V3.0 优化修改
原作者博客:https://blog.dyboy.cn/ Monkey V3.0 优化修改版 修改说明: 背景颜色修改(按个人喜好可自行修改,仿PCQQ午夜巴黎皮肤) 搜索框按钮样式优化,不那么突兀了 ...
- Learning Spark中文版--第四章--使用键值对(2)
Actions Available on Pair RDDs (键值对RDD可用的action) 和transformation(转换)一样,键值对RDD也可以使用基础RDD上的action(开工 ...
- windows Visual Studio 上安装 CUDA【转载】
原文 : http://blog.csdn.net/augusdi/article/details/12527497 前提安装: Visual Studio 2012 Visual Assist X ...
- HTTP初识
HTTP(HyperText Transfer Protocol):超文本传输协议. URL(Uniform Resource Locator):统一资源定位符. URI(Uniform Resour ...
- [项目总结]关于调用系统照相机Activity被销毁问题解决
在项目中需要启用系统照相机来拍照.本来很容易的一个问题.但在适配中出现了问题. 简单说一下问题: 有些手机拍照成功,有些手机拍完照后确定返回后activity数据丢失,被销毁了. 问题查找: 经过代码 ...
- 利用Lombok编写优雅的spring依赖注入代码,去掉繁人的@Autowired
大家平时使用spring依赖注入,都是怎么写的? @Servicepublic class OrderService {@Autowiredprivate UserService userServic ...
- 在html页面通过绝对地址显示图片
1.编辑tomcat中conf目录下的server.xml文件,在<Host></Host>中添加如下代码段 <Context path="/D" d ...