题目如下:

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.

The encoded string should be as compact as possible.

Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

解题思路:我的序列化的方法是"根左右"的顺序进行遍历,每个节点的值之间以'#'分割。那么在反序列化的时候,首先把序列化的结果按'#'分割成数组,很显然数组的第一个元素是根节点,同时遍历数组,找到第一个比根节点值大的节点,这个节点左边的元素属于根节点的左子树,自身及右边的元素属于根节点的右子树。接下来分别对左右子树递归,直到所有节点的构造完成为止。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Codec: def serialize(self, root):
"""Encodes a tree to a single string. :type root: TreeNode
:rtype: str
"""
self.ser = ''
def recurisve(node):
if node == None:
return
self.ser += '#'
self.ser += str(node.val)
recurisve(node.left)
recurisve(node.right)
recurisve(root)
return self.ser[1:] def deserialize(self, data):
"""Decodes your encoded data to tree. :type data: str
:rtype: TreeNode
"""
if len(data) == 0:
return None
val_list = [int(i) for i in data.split('#')]
root = TreeNode(-1)
def build(node,path):
if len(path) == 0:
return
rv = path[0]
left = []
inx = 0
for i in range(1,len(path)):
if path[i] < rv:
inx = i
left.append(path[i])
else:
break
right = path[inx+1:]
node.val = rv
if len(left) > 0:
node.left = TreeNode(-1)
build(node.left,left)
if len(right) > 0:
node.right = TreeNode(-1)
build(node.right, right)
build(root,val_list)
return root # Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))

【leetcode】449. Serialize and Deserialize BST的更多相关文章

  1. 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)

    [LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...

  2. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  3. 【LeetCode】297. Serialize and Deserialize Binary Tree

    二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...

  4. [leetcode]449. Serialize and Deserialize BST序列化与反序列化BST

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  5. [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. LeetCode 449. Serialize and Deserialize BST

    原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...

  7. 449. Serialize and Deserialize BST

    https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...

  8. 449. Serialize and Deserialize BST——几乎所有树的面试题目都会回到BFS或者DFS,使用BFS,None节点存#

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. [leetcode]449. Serialize and Deserialize BST设计BST的编解码

    这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以 唯一得确定. ...

随机推荐

  1. activiti 流程发起人控制

    最近做activiti流程发起人的控制,最开始的想法是新建一张表 ,通过控制流程定义id与发起人id进行控制,如果这样每次发布新的流程就必须 重新设置流程发起人,因为通过流程定义不能获取流程模型id, ...

  2. 010-elasticsearch5.4.3【四】-聚合操作【一】-度量聚合【metrics】-min、max、sum、avg、count

    一.概述 度量类型聚合主要针对的number类型的数据,需要ES做比较多的计算工作 参考向导:地址 import org.elasticsearch.search.aggregations.Aggre ...

  3. django中间件(获取请求ip)

    def simple_middleware(get_response): # 此处编写的代码仅在Django第一次配置和初始化的时候执行一次. print('1----django启动了') def ...

  4. PHP 距离我最近排序+二维数组按指定列排序

    思路: 1.获取我的位置,即:我的经纬度 2.各站点须有位置     即:排序对象有位置经纬度 3.查询要排序的站点列表 4.循环遍历计算  与我的距离 5.二维数组按 指定列(距离)排序 具体如下: ...

  5. idea下载和设置自动翻译(有道)

    1:下载 点击file,点击settings,找到plugins,之后所搜translation并下载,他会自动从新启动idea 2:设置translation 3:这个应用ID和秘钥需要在有道智云去 ...

  6. vsphere虚拟化之Active Directory域的创建(一)

    1.搭建环境说明 本机是在Vmware Workstation 12 Pro虚拟软件下进行搭建的. 操作系统版本:Windows Server 2012 R2 简体中文企业版x64. 2.安装完win ...

  7. CSRF verification failed. Request aborted.错误解决办法

    在Django项目的页面,提交form表单POST请求时,会出现报错:CSRF verification failed. Request aborted. 需要在form表单中加:{% csrf_to ...

  8. 关于AndroidStudio 配置的默认路径的修改

    AndroidStudio的配置默认路径在 C:\Users\用户名\.AndroidStudio3.0 下,在这里会有一个缺点是C盘会常常空间不够用,所以我就想改到其他盘的.看图: Android ...

  9. C#开发 WinForm如何在选项卡中集成加载多个窗体 实现窗体复用

    http://blog.csdn.net/upi2u/article/details/37914909 最近需要做的一个项目,为了避免从菜单中选择的麻烦,需要把几个窗体集成到一起,通过TabContr ...

  10. C# 中常见的控件以及功能

    1.StatusBar控件——显示各种状态信息. StatusBar控件可以有状态栏面板(用于显示图标以指示状态)或一系列动画图标(用于指示某个进程正在工作,例如,表示正在保存文档的 Microsof ...