当前位置:网站首页>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
边栏推荐
- Implementation principle of dynamic agent
- Sword finger offer 15 Number of 1 in binary
- Solr基础操作4
- Gradle serialization 7- configuration signature
- label问题排查:打不开标注好的图像
- 软件测试 接口测试 Postman测试工具 接口测试的流程 执行接口测试 接口关联 环境变量和全局变量 内置动态参数以及自动有的动态参数
- Sword finger offer 14- I. cut rope
- [译]在软件开发行业工作 6 年后,那些年我曾改过的观念
- 大厂试水 HPE推出Arm CPU通用服务器产品
- Dépannage de l'étiquette: impossible d'ouvrir l'image marquée
猜你喜欢

Paper writing tool: latex online website

CE second operation

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

M1笔记本居家办公的痛点及解决方案 | 社区征文

“微博评论”的高性能高可用计算架构

HPE launched ARM CPU general server product

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

Remember the process of checking online MySQL deadlock. You should not only know curd, but also know the principle of locking

均值、方差、标准差、协方差的概念及意义

软件测试 接口测试 Jmeter 5.5 安装教程
随机推荐
AI赋能新零售,「智」胜之道在于生态思维|数智夜话直播精选摘录
搭建企业级NTP时间服务器
Gradle serialization 7- configuration signature
绿树公司官方网站
Sword finger offer 14- I. cut rope
Test d'installation du cluster metaq
111. simple chat room 14: chat room client
软件测试 接口测试 Jmeter 5.5 安装教程
穿越过后,她说多元宇宙真的存在
Metaq cluster installation test
[wechat applet] understand the basic composition of the applet project
Solution to version conflict of flutter plug-in
Use of jetpack's room in combination with flow
數莓派 4怎麼樣?可能的玩法有哪些?
Leetcode(680)——验证回文字符串 Ⅱ
LC: effective Sudoku + rotating image
Solr basic operation 4
Bee常用配置
RRDtool 画MRTG Log数据
Implementation principle of dynamic agent