zoukankan      html  css  js  c++  java
  • 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'
     
  • 相关阅读:
    kafka 官方 Quickstart
    oracle11.2 安装
    Perl参考函数/教程
    Mysql参见SHOW命令总结
    MySQL的Innodb缓存相关优化
    Oracle、Mysql和SQL Server数据库连接的URL写法
    jredis 客户端 使用
    sql基本命令-存储过程
    NoSql系列目录ElasticSearch-mongodb
    loadrunner 运行场景-Controller及Load Generators宿主主机优化
  • 原文地址:https://www.cnblogs.com/rosyYY/p/10980268.html
Copyright © 2011-2022 走看看