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的更多相关文章

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

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

  2. [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 ...

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

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

  4. 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 ...

  5. LeetCode 449. Serialize and Deserialize BST

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

  6. 【leetcode】449. Serialize and Deserialize BST

    题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...

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

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

  8. 449 Serialize and Deserialize BST 序列化和反序列化二叉搜索树

    详见:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ C++: /** * Definition fo ...

  9. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

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

随机推荐

  1. FileItem类的常用方法(关于文件上传的)

    1.boolean  isFormField().isFormField方法用来判断FileItem对象里面封装的数据是一个普通文本表单字段,还是一个文件表单字段.如果是普通文本表单字段,返回一个tr ...

  2. 六 json&pickle模块

    之前我们学习过用eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型,json.loads和eval都能用,但遇到特殊类型的时候,eval就不管用了 ...

  3. pta l2-16(愿天下有情人都是失散多年的兄妹)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805061769609216 题意:两个异性的人五服之内不得通婚 ...

  4. ubuntu下java的安装即使用

    1.首先在官方网站(点击可以下载)下载最新的JDK,要选用self extracting installer 2.在/usr/下新建java目录,把下载的文件放到这个目录下 sudo mkdir /u ...

  5. 排列组合或容斥原理 SPOJ - AMR11H

    题目链接: https://vjudge.net/contest/237052#problem/H 这里给你一串数字,让你计算同时拥有这串数字最大值和最小值的子集(连续)和子序列(可以不连续)的数量, ...

  6. Northwestern European Regional Contest 2016 NWERC ,F题Free Weights(优先队列+Map标记+模拟)

    传送门: Vjudge:https://vjudge.net/problem/Gym-101170F CF: http://codeforces.com/gym/101170 The city of ...

  7. 100. Same Tree (Tree;DFS)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  8. Codeforces Beta Round #46 (Div. 2)

    Codeforces Beta Round #46 (Div. 2) http://codeforces.com/contest/49 A #include<bits/stdc++.h> ...

  9. 【linux】进程状态

    [进程状态转移图] PROCESS STATE CODES R running or runnable (on run queue) D uninterruptible sleep (usually ...

  10. Delphi:基于jcl的Bugsplat Crash收集单元

    //BugSplat Crash模拟.net数据封装 unit uBugSplat; interface uses Windows, SysUtils, Classes, StrUtils, Shel ...