当前位置:网站首页>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
边栏推荐
- The concept and significance of mean, variance, standard deviation and covariance
- Bee常用配置
- SQL question brushing 595 Big country
- High performance and high availability computing architecture of "Weibo comments"
- 6.28日刷题题解
- Cacti最大监控数测试
- 海外数字身份验证服务商ADVANCE.AI入选EqualOcean《2022品牌出海服务市场研究报告》
- [LeetCode] 只出现一次的数字【136】
- 开始“收割”!钉钉调整“钉钉Teambition”免费用人数上限,超十人将无法正常用
- MetaQ集群安装测试
猜你喜欢

High performance and high availability computing architecture of "microblog comments" in microblog system

【一起上水硕系列】Day 8

采购数智化爆发在即,支出宝“3+2“体系助力企业打造核心竞争优势

C pointer advanced 2-- > function pointer array callback function simplifies calculator code, and implements qsort function based on callback function simulation

剑指 Offer 13. 机器人的运动范围

软件测试 接口测试 Postman测试工具 接口测试的流程 执行接口测试 接口关联 环境变量和全局变量 内置动态参数以及自动有的动态参数

按头安利!好看又实用的电机 SolidWorks模型素材看这里

Collection! Have you ever used these tools to improve programmer productivity?

@Scheduled注解的坑,我替你踩了

新钛云服荣膺“2022爱分析 · IT运维厂商全景报告”云管理平台CMP 代表厂商!...
随机推荐
6.29 problem solving
海外数字身份验证服务商ADVANCE.AI入选EqualOcean《2022品牌出海服务市场研究报告》
InfluxDB时序数据库系统
Shell positional parameter variables and predefined variables
Sword finger offer 15 Number of 1 in binary
MetaQ集群安裝測試
Head pressing Amway good-looking and practical dispensing machine SolidWorks model material here
Construction of module 5 of actual combat Battalion
Gradle连载7-配置签名
Sword finger offer 13 Range of motion of robot
Divisor
Et la tarte aux framboises 4? Quels sont les jeux possibles?
matplotlib matplotlib可视化之柱状图plt.bar()
小程序插件接入、开发与注意事项
Which securities company should I choose to open an account online? Also, is it safe to open an account online?
333333333333333333333333333333
这个简单的小功能,半年为我们产研团队省下213个小时
剑指 Offer 14- II. 剪绳子 II
Remember the process of checking online MySQL deadlock. You should not only know curd, but also know the principle of locking
按头安利 好看又实用的点胶机 SolidWorks模型素材看这里