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 ... 
随机推荐
- js自执行函数
			5.1对于函数表达式,在后面加括号即可以让函数立即执行:例如下面这个函数,至于为什么加了括号就可以立即执行,我们可以这么理解,就是像fn1():这样写的话,函数 可以立即执行是没问题的,我们在经常会用 ... 
- springboot在集成mybatis的时候老是报错 The server time zone value '�й���ʱ��' is unrecognized
			我已经解决了,感谢万能网友. 解决办法参见:https://blog.csdn.net/yunfeng482/article/details/86698133 
- DNS 缓存投毒
			原文:[DNS Cache Poisoning]( https://medium.com/iocscan/dns-cache-poisoning-bea939b5afaf) 译者:neal1991 w ... 
- 简单了解node stream
			Almost all Node.js applications, no matter how simple, use streams in some manner. 开篇先吓吓自己.画画图,分析分析代 ... 
- C语言面试相关知识点
			1.关键字static的作用是什么? 有三个明显的作用: 1)在函数体内,一个被声明为静态的变量在这个函数被调用过程中维持其值不变 2)在模块内(但在函数体外),静态的变量可以被模块内所有函数访问,但 ... 
- poj 1664 放苹果(dfs)
			放苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30284 Accepted: 19098 Description ... 
- DigitalOcean 推荐的ubuntu16下LAMP安装过程
			LAMP安装过程: How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04 (另一个参考例程:Ubuntu 16.0 ... 
- Codeforces 979 字符串强制N变换最多出现字母 DFS子树 暴力01字典树
			A /* Huyyt */ #include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mkp(a,b) ... 
- Angular7和PrimeNg集成
			常规操作之后,随便加了一个控件发现报错了.错误信息看起来是不能识别PrimeNg的组件,经过一番折腾发现.因为用到了ngModel,需要导入FormsModule.因为新建的工程没有导入,导入之后就好 ... 
- [uboot] (第二章)uboot流程——uboot-spl编译流程(转)
			版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/ooonebook/article/det ... 
