当前位置:网站首页>297. Serialize and Deserialize Binary Tree
297. Serialize and Deserialize Binary Tree
2022-06-23 08:24:00 【ujn20161222】
297. Serialize and Deserialize Binary Tree
Hard
6257245Add to ListShare
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.
Clarification: The input/output 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.
Example 1:

Input: root = [1,2,3,null,null,4,5] Output: [1,2,3,null,null,4,5]
Example 2:
Input: root = [] Output: []
Constraints:
- The number of nodes in the tree is in the range
[0, 104]. -1000 <= Node.val <= 1000
# 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 doit(node):
if node:
vals.append(str(node.val))
doit(node.left)
doit(node.right)
else:
vals.append("#")
doit(root)
return ' '.join(vals)
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
def doit():
val=next(vals)
if val=="#":
return None
node=TreeNode(int(val))
node.left=doit()
node.right=doit()
return node
vals=iter(data.split())
return doit()
# Your Codec object will be instantiated and called as such:
# ser = Codec()
# deser = Codec()
# ans = deser.deserialize(ser.serialize(root))相当于遍历二叉树
边栏推荐
- Android kotlin coroutines KTX extension
- Use newbeecoder UI implements data paging
- Go 数据类型篇(三)之整型及运算符
- 3. Caller 服务调用 - dapr
- What is a dedicated server line
- Unique paths II of leetcode topic analysis
- 2-用线段构成图形、坐标转换
- ArcLayoutView: 一个弧形布局的实现
- “方脸老师”董宇辉再回应热度下降:把农产品直播做好让农民受益 考虑去支教
- Easycvr accesses the website through the domain name. How to solve the problem that the video cannot be viewed back?
猜你喜欢

渲染效果图哪家好?2022最新实测(四)

最常用的5中流ETL模式

173. Binary Search Tree Iterator

Point cloud library PCL from introduction to mastery Chapter 10
![Vulnhub | DC: 4 | [combat]](/img/33/b7422bdb18f39e9eb55855dbf1d584.png)
Vulnhub | DC: 4 | [combat]
![[cloud computing] GFS ideological advantages and architecture](/img/98/2a4ef0ca805add24d431dac9808903.png)
[cloud computing] GFS ideological advantages and architecture

The most commonly used 5-stream ETL mode

给你的win10装一个wget

986. Interval List Intersections

typeScript的介绍与变量定义的基本类型
随机推荐
C Advanced Learning -- extended method (this)
单编内核驱动模块
How can I handle the "unable to load" exception when easyplayer plays webrtcs?
Why use growth neural gas network (GNG)?
[cloud computing] GFS ideological advantages and architecture
Arthas vmtool命令小结
MySQL common skills
When easynvr service is started, video cannot be played due to anti-virus software interception. How to deal with it?
Set interface and set sub implementation classes
2- use line segments to form graphics and coordinate conversion
Derivation and loading of the trained random forest model
Idea true permanent activation method and permanent activation code tutorial
Pyspark on HPC (Continued): reasonable partition processing and consolidated output of a single file
usb peripheral 驱动 - configfs
Implementing an open source app store with swiftui
Object.defineProperty() 和 数据代理
Go 数据类型篇(二)之Go 支持的数据类型概述及布尔类型
6月《中國數據庫行業分析報告》發布!智能風起,列存更生
你有一串代码,但是不支持低版本Go时;判断Go版本号,您值得拥有!
史上最污技术解读,60 个 IT 术语我居然秒懂了......