【leetcode】449. Serialize and Deserialize BST
题目如下:
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的更多相关文章
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree
二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...
- [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 ...
- [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode 449. Serialize and Deserialize BST
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
- 449. Serialize and Deserialize BST
https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...
- 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 ...
- [leetcode]449. Serialize and Deserialize BST设计BST的编解码
这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以 唯一得确定. ...
随机推荐
- optistruct线性求解一次二次单元应力位移比较
通过分析比较10mm.5mm.3mm.1mm的网格模型, 网格越细密: 位移与应力均趋于恒定值(收敛): 一次与二次单元的应力区域一致: 一次与二次单元的位移相差11.3%,一次单元的位移小. 所用的 ...
- day51—JavaScript绑定事件
转换学开发,代码100天——2018-05-06 今天学习JavaScript的绑定事件.因为浏览器的原因绑定事件需要考虑兼容性问题. attachEvent IE浏览器 ,ie9以上事件执行 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第4节 ArrayList集合_19-ArrayList练习四_筛选集合
大集合里面循环装了20个int类型的随即数字 下面要自定义方法,这个方法专门负责筛选 遍历偶数的集合 重点是集合当做方法的参数,还能当做集合的返回值
- git_02_git常用操作命令
前言 Git是一个开源的分布式版本控制系统,可以有效.高速地处理从小到大的项目版本管理.编写自动化测试脚本的过程中,经常要用到git命令,但总是记不住,每次都要百度有些麻烦.于是为了方便使用,在这总结 ...
- Python笔记(二十八)_魔法方法_迭代器
迭代器用于遍历容器中的数据,但它不是容器,它是一个实现了__next__方法的对象 与迭代器相关的内置函数: iter(): 将一个对象转换成一个迭代器 next(): 访问迭代器中的下一个变量,直到 ...
- 【HANA系列】SAP HANA DB 和SAP HANA studio version查看
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA DB 和SAP ...
- JS和C#后台获取网站URL
例:网页URL : http://localhost:8086/index.aspx?topicId=361 1.设置或获取 href 属性中跟在问号后面的部分:window.location.se ...
- Ansible--常用命令整理
由于最近使用ansible在多台服务器部署程序,运行命令的时候,发现对Linux和ansible自动运维工具用的不太熟练,所以搜集整理一些,方便日后复习提升,达到熟练运用的目的. 对于详细的安装教程和 ...
- CentOSLinux系统中Ansible自动化运维的安装以及利用Ansible部署JDK和Hadoop
Ansible 安装和配置 Ansible 说明 Ansible 官网:https://www.ansible.com/ Ansible 官网 Github:https://github.com/an ...
- CTF夺旗赛丨网络内生安全试验场第四季圣诞赛明日开赛!
期待许久的圣诞狂欢就要来喽 <Jingle bell>欢快的旋律 在耳边翩翩起舞 白胡子老爷爷骑着麋鹿准时来送礼物 2019圣诞节 i 春秋做你的圣诞老人 参加CTF欢乐圣诞赛 你提交答案 ...