leetcode-mid-design-297. Serialize and Deserialize Binary Tree¶-NO -??
mycode
将list转换成树的时候没有思路
参考:
deque 是双边队列(double-ended queue),具有队列和栈的性质,在 list 的基础上增加了移动、旋转和增删等
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))
其中queue可以用list替换
def deserialize(self, data):
"""Decodes your encoded data to tree. :type data: str
:rtype: TreeNode
"""
print(data)
self.vals = [val for val in data.split()]
print(self.vals)
def build():
if self.vals:
val = self.vals[0]
self.vals = self.vals[1:] if val == '#':
return None
root = TreeNode(int(val))
root.left = build()
root.right = build()
return root
return build()
疑惑
def deserialize(self, data):
"""Decodes your encoded data to tree. :type data: str
:rtype: TreeNode
"""
print(data)
vals = [val for val in data.split()]
print(self.vals)
def build():
if vals:
val = vals[0]
vals[:] = vals[1:]
print(vals)
if val == '#':
return None
root = TreeNode(int(val))
root.left = build()
root.right = build()
return root
return build()
Line 35: AttributeError: Codec instance has no attribute 'vals'
leetcode-mid-design-297. Serialize and Deserialize Binary Tree¶-NO -??的更多相关文章
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- LC 297 Serialize and Deserialize Binary Tree
问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...
- 297. Serialize and Deserialize Binary Tree
题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...
- 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 序列化与反序列化二叉树
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
二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...
- 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 ...
- 297. Serialize and Deserialize Binary Tree二叉树的序列化和反序列化(就用Q)
[抄题]: Serialization is the process of converting a data structure or object into a sequence of bits ...
随机推荐
- mybatis中的动态代理应用(mapper对象)
-----------------UserMapper的配置信息--------------------- <?xml version="1.0" encoding=&quo ...
- 和IE6-IE8说拜拜 一段IE兼容HTML代码 针对IE版本写css
通过这段html可以让你对特定的ie版本添加内容,只在特定版本ie展现,可以是javascript.css.html. <!--[if IE]> 这样使用IE浏览器(全部版本)的人都看得到 ...
- MySQL第二讲 一一一一 MySQL语句进阶
通过命令来备份数据库: 通过数据库软件里面的,mysqldump模块来操作,如下: mysqldump -u root db1 > db1.sql -p; //没有-d就是备份的时候:数据表结构 ...
- python grobal 的使用方法
写一个功能,运行报错,name 'number' is used prior to global declaration ,查资料梳理一下 因为这个函数需要调用多次,第一次调用的时候,走if语句,后面 ...
- 批量修改zencart产品价格、原价、特价、产品属性价格
批量修改zencart商品价格无非只有下面几种情况: 一 在原来基础上批量调高一定比例 二 将原来的价格批量换成一个新的价格 针对第一种情况的话,网上很多人已经给出了解决办法: 利用SQL语句批量修改 ...
- 前端matrix矩阵的变化
css3 transform中的matrix矩阵 CSS3中的矩阵CSS3中的矩阵指的是一个方法,书写为matrix()和matrix3d(),前者是元素2D平面的移动变换(transform), ...
- SQL优化之表连接方式
1.嵌套循环(DESTED LOOPS) Note:嵌套循环被驱动表必须走索引,而且索引只能走INDEX UNIQUE SCAN或者INDEX RANGE SCAN SQL> select /* ...
- vue 无缝滚动文字
前言 用vue做无缝滚动,字体弹幕 就上代码吧 <head> <meta charset="UTF-8"> <style> div, ul, l ...
- form表单和CSS
一.form表单 1. form表单有什么用 能够获取用户输入的信息(输入,选择, 上传的文件),并且将这些数据全部发送给后端 2. form表单的用法 (1)有两个重要参数: action : 控制 ...
- Gym-100923H-Por Costel and the Match(带权并查集)
链接: https://vjudge.net/problem/Gym-100923H 题意: Oberyn Martell and Gregor Clegane are dueling in a tr ...