【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,先序遍历确实是可以 唯一得确定. ...
随机推荐
- leetcode-mid-Linked list-2 Add Two Numbers
mycode 87.22% # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x) ...
- ContentProvider,ContentResolver和ContentObserver 使用
1 ContentProvider内容提供者 四大组件之一,实现不同程序实现数据的共享.联系人应用就使用了ContentProvider,比如你在自己的应用可以读取和修改联系人的数据(获得相应权限). ...
- TreeSet 源码分析
TreeSet 1)底层由 TreeMap 支持的 Set 接口实现,Set 中的元素按照自然顺序或指定的比较器排序. 创建实例 /** * 支持此 Set 的底层的 TreeMap 对象 */ pr ...
- 013-Spring Boot web【二】静态资源、Servlet、Filter、listenter
一.静态资源 1.1.webapp默认支持静态资源 在src/main/webapp下建立user.html默认支持访问 1.2.默认内置静态资源目录.可被直接访问 查看包:spring-boot-a ...
- 红帽虚拟化RHEV-PXE批量安装RHEV-H
目录 目录 前言 软件环境 前提 部署 PXE 环境 使用 yum 安装所需软件 配置 DHCP 配置 TFTP-Server 配置 vsftpd 服务用于提供安装系统所需软件包 安装 kicksta ...
- Golang new() vs make()
对于Golang的new() 和 make()的用法有些混乱,感觉这篇资料讲解较好,翻译一下,方便学习! 原文地址:http://www.godesignpatterns.com/2014/04/ne ...
- MySQL使用Navicat远程连接时报错1251
1.报错信息 client does not support authentication protocol requested by server:consider upgrading MySQL ...
- django基于odm,简单的post和get封装
- Echars -- 在Vue中如何使用Echars
在vue-cli项目中使用echarts -->Wangqi 这个示例使用 vue-cli 脚手架搭建 安装echarts依赖 npm install echarts -S 或者使用国内的淘 ...
- c语言秋季作业1
1:你对软件工程专业或者计算机科学与技术专业了解是怎样? answer:据我上网了解软件工程是一门研究用工程化方法构建和维护有效的.实用的和高质量的软件的学科.它涉及程序设计语言.数据库.软件开发工具 ...