449. Serialize and Deserialize BST
https://leetcode.com/problems/serialize-and-deserialize-bst/#/description
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.
Sol:
Serialize:
Use preorder to keep the BST data structure and store data in a list as strings.
Deserialize:
Pop elements from the serialization list and biuld new nodes in a preorder manner.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Codec: # Time O(n)
def serialize(self, root):
"""Encodes a tree to a single string. :type root: TreeNode
:rtype: str
"""
vals = [] def preOrder(node):
if node:
vals.append(node.val)
preOrder(node.left)
preOrder(node.right) preOrder(root) return ' '.join(map(str, vals)) def deserialize(self, data):
"""Decodes your encoded data to tree. :type data: str
:rtype: TreeNode
"""
vals = collections.deque(int(val) for val in data.split()) def build(minVal, maxVal):
if vals and minVal < vals[0] < maxVal:
val = vals.popleft()
node = TreeNode(val)
node.left = build(minVal, val)
node.right = build(val, maxVal)
return node return build(float('-INF'), float('INF')) # Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))
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]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 ...
- 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
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
- 【leetcode】449. Serialize and Deserialize BST
题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...
- [leetcode]449. Serialize and Deserialize BST设计BST的编解码
这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以 唯一得确定. ...
- 449 Serialize and Deserialize BST 序列化和反序列化二叉搜索树
详见:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ C++: /** * Definition fo ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- php 更新array键值
$arr1 = array("loginname" => "username","psw" => "password& ...
- 齐博CMS:最常用的一些变量名,方便二次开发.必须要熟悉的
ROOT_PATH程序所在硬盘目录的绝对地址,等同于以前的PHP168_PATH$webdb网站的全局变量,模块的全局变量也是这个$onlineip当前用户的IP$timestamp当前时间$WEBU ...
- 织梦 列表页 list标签 按照自已设置的方式排序
一.可以按照权重排序 降序排序 desc 1.添加的文章默认权重是自动加1,所以只要把想置顶的文章权重设置很高,如10000 2.{dede:list pagesize='12′ orderby='w ...
- 从上往下打印二叉树(python)
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. # -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # ...
- 第十一章 串 (c1)KMP算法:从记忆力到预知力
- C#的扩展方法解说
扩展方法的目的就是为一个现有类型添加一个方法,现有类型既可以是int,string等数据类型,也可以是自定义的数据类型. 为数据类型的添加一个方法的理解:一般来说,int数据类型有个Tostring的 ...
- tcp/ip通信第5期之客户机端程序
/*此程序是tcp/ip通信的客户机端程序, 测试运行在redhat6系统上 重构readline函数,解决粘包问题——利用“\n”识别一个消息边界 */ #include<stdio.h> ...
- aruba 802.11ac协议
上述功能为802.11ac协议,高密环境下建议不勾选. 附百度百科:虽然802.11ac标准草案提高了传输速度并增加了带宽,可以支持企业网络中数量越来越庞大的设备,但是企业开始发现,这个标准需要依赖于 ...
- easyui的dialog
代码: <div id="titledialos" class="easyui-dialog" title="×××" data-op ...
- iOS - 抖音效果
抖音的转场动画—iOS https://www.jianshu.com/p/29b0165de712 抖音的上下滑实现—iOS https://www.jianshu.com/p/e8799510c7 ...