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 ...
随机推荐
- docker:相关命令
1.查看正在运行的容器 docker ps docker ps -a 查看所有的容器,包括已经停止了的 2.WORKDIR Dockerfile中的WORKDIR指令用于指定容器的一个目录,容器启动时 ...
- Android中res下anim和animator文件夹区别与总结
1.anim文件夹 anim文件夹下存放tween animation(补间动画)和frame animation(逐帧动画) 逐帧动画: ①在animation-list中使用item定义动画的全部 ...
- 利用ARouter实现组件间通信,解决子模块调用主模块问题
如果你还没使用过ARouter请你按照这篇下面博客尝试使用下然后再往下看组件通信的内容(不然的话可能会懵逼)Android Studio接入ARouter以及简单使用 如果你使用过ARouter请继续 ...
- 软件开发中oracle查询常用方法总结
上次新霸哥和大家讲解了一些关于oracle的知识发现大家对oracle还是比较感兴趣的,下面新霸哥就大家比较关系的oracle中常用的查询有哪几种?做个和oracle相关的开发的朋友可能会知道答案,但 ...
- linux命令详解——tar
tar [-cxtzjvfpPN] 文件与目录 .... [参数]: -c :建立一个压缩文件的参数指令(create 的意思): -x :解开一个压缩文件的参数指令! -t :查看 tarfile ...
- 魔术矩阵Java代码
//该魔术矩阵默认从右上角45度递增 //@漫流——595128841在qq点com //import java.util.Arrays; //用于打印API函数 public class 魔方矩阵 ...
- Min-max theorem
wiki链接
- LCA统计
读入挂 inline void read(int &v) { v = ; ; ; ') { if (c == '-') { p = -; } c = getchar(); } ') { v = ...
- Gym-100814K 数位DP 模拟除法
Johnny is a brilliant mathematics student. He loves mathematics since he was a child, now he is work ...
- 补充:bytes类型以及字符编码转换
内容转自小猿圈链接:https://book.apeland.cn/details/41/ 定义 bytes类型是指一堆字节的集合,在python中以b开头的字符串都是bytes类型 b'\xe5\x ...