当前位置:网站首页>Serialization of binary tree 297 Serialization and deserialization of binary tree 652 Find duplicate subtrees
Serialization of binary tree 297 Serialization and deserialization of binary tree 652 Find duplicate subtrees
2022-06-29 23:51:00 【Steven Devin】
Serialization and deserialization of binary trees
297. Serialization and deserialization of binary trees

Their thinking :
Make use of the above , The idea of decomposition .
serialize : Post order traversal of concatenated strings .
Deserialization : Because you want to split the comma of the string , Additional one helper Function to recursively decompose and splice into a tree .
class Codec:
def serialize(self, root):
"""Encodes a tree to a single string. :type root: TreeNode :rtype: str """
if not root:
return 'None'
left = self.serialize(root.left)
right = self.serialize(root.right)
return str(root.val) + ',' + str(left) +','+ str(right)
def deserialize(self, data):
"""Decodes your encoded data to tree. :type data: str :rtype: TreeNode """
data = data.split(',')
root = self.helper(deque(data))
return root
def helper(self,data):
val=data.popleft()
if val=='None':
return None
root = TreeNode(int(val))
root.left = self.helper(data)
root.right = self.helper(data)
return roott
652. Look for duplicate subtrees

Their thinking :
First serialize the tree into a string .
Post order traversal compares repeated strings .
class Solution:
def findDuplicateSubtrees(self, root: Optional[TreeNode]) -> List[Optional[TreeNode]]:
""" Serialize the sequence of subtrees of the entire tree , If such a subtree already exists , The output """
self.res = []
self.counter = Counter()
self.traverse(root)
return self.res
def traverse(self, root):
if not root:
return None
left = self.traverse(root.left)
right = self.traverse(root.right)
# Serialized string
chain = str(root.val) + ',' + str(left) + ',' + str(right)
# The dictionary records every string
self.counter[chain] += 1
#list Record duplicate structure string in
if self.counter[chain] == 2:
self.res.append(root)
return chain
边栏推荐
- On binary tree
- LC: maximum subarray and
- After working in the software development industry for six years, I changed my ideas in those years
- 软件测试 接口测试 Postman测试工具 接口测试的流程 执行接口测试 接口关联 环境变量和全局变量 内置动态参数以及自动有的动态参数
- Under the epidemic, I left my job for a year, and my income increased 10 times
- Solr基础操作1
- 搭建企业级NTP时间服务器
- 软件测试 接口测试 Jmeter 5.5 安装教程
- Shell operator
- 【微信小程序】认识小程序项目的基本组成结构
猜你喜欢

Matlab exercises -- program control process exercise

Yunhe enmo Guoqiang, identifiez - le, saisissez - le, avant l'ébullition de la base de données nationale

软件测试 接口测试 Jmeter 5.5 安装教程

Head pressing Amway good-looking and practical dispensing machine SolidWorks model material here

Overseas digital authentication service provider advance AI was selected into the "2022 brand sea Service Market Research Report" of equalocean

Procurement intelligence is about to break out, and the "3+2" system of Alipay helps enterprises build core competitive advantages

Paper writing tool: latex online website

云和恩墨盖国强,识别它、抓住它,在国产数据库沸腾以前

High performance and high availability computing architecture of "Weibo comments"

均值、方差、标准差、协方差的概念及意义
随机推荐
Jetpack之Room的使用,结合Flow
Overseas digital authentication service provider advance AI was selected into the "2022 brand sea Service Market Research Report" of equalocean
Official website of Greentree
The concept and significance of mean, variance, standard deviation and covariance
Solr基础操作1
深度学习的历史
Regular expressions: characters (2)
HPE launched ARM CPU general server product
High performance and high availability computing architecture of "microblog comments" in microblog system
Shell positional parameter variables and predefined variables
Pytest initializing and cleaning up the environment
Cacti最大监控数测试
6.29 problem solving
FPGA开发(1)——串口通信
记一次排查线上MySQL死锁过程,不能只会curd,还要知道加锁原理
Software testing interface testing JMeter 5.5 installation tutorial
Test d'installation du cluster metaq
我想知道今天还可以开户么?另外想问,现在在线开户安全么?
声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏
Pain points and solutions of M1 notebook home office | community essay solicitation