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.

DFS

# 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
"""
if root is None:
return "#"
return str(root.val) + " " + self.serialize(root.left) + " " + self.serialize(root.right) def deserialize(self, data):
"""Decodes your encoded data to tree. :type data: str
:rtype: TreeNode
"""
data = data.split()
return self.deserialize_helper(data, []) def deserialize_helper(self, data, i):
if data[i[]] == "#":
return None
root = TreeNode(int(data[i[]]))
i[] +=
root.left = self.deserialize_helper(data, i)
i[] +=
root.right = self.deserialize_helper(data, i)
return root # Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))

DFS 2:

# 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
"""
if root is None:
return "#"
return str(root.val) + " " + self.serialize(root.left) + " " + self.serialize(root.right) def deserialize(self, data):
"""Decodes your encoded data to tree. :type data: str
:rtype: TreeNode
"""
data = data.split()
if data[0] == '#':
return None
node = root = TreeNode(int(data[0]))
stack = [root]
i = 1
while data[i] != '#':
node.left = TreeNode(int(data[i]))
stack.append(node.left)
node = node.left
i += 1
while stack:
node = stack.pop()
i += 1
if data[i] != '#':
node.right = TreeNode(int(data[i]))
stack.append(node.right)
node = node.right
i += 1
while data[i] != '#':
node.left = TreeNode(int(data[i]))
stack.append(node.left)
node = node.left
i += 1
else:
node.right = None
return root # Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))

BFS

# 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
"""
if root is None:
return ""
stack = [root]
ans = str(root.val)
while stack:
stack2 = []
for node in stack:
if node.left:
stack2.append(node.left)
ans += " "+str(node.left.val)
else:
ans += " #"
if node.right:
stack2.append(node.right)
ans += " " + str(node.right.val)
else:
ans += " #"
stack = stack2
return ans def deserialize(self, data):
"""Decodes your encoded data to tree. :type data: str
:rtype: TreeNode
"""
array = data.split()
if not array:
return None
root = TreeNode(int(array[]))
stack = [root]
i =
while stack:
stack2 = []
for node in stack:
if array[i] != '#':
node.left = TreeNode(int(array[i]))
stack2.append(node.left)
else:
node.left = None
i +=
if array[i] != '#':
node.right = TreeNode(int(array[i]))
stack2.append(node.right)
else:
node.right = None
i +=
stack = stack2
return root # Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))

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

  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

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

  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. JavaScript 拼接JSON

    <script language="javascript" type="text/javascript"> var json="" ...

  2. C#打印条码的几种方式

    标题虽然是说C#,但是以下介绍的几种方法不是只能在C#中使用,在其它的语言里面也行. 总结一下常见的条码打印方法,其实打条码的方式很多,大概有以下几种: 1.斑马打印软件制作好模板,保存为.prn格式 ...

  3. SqlServer 一些操作

    //查询一个表中的某字段为条件修改另一个表的内容 update [VehicleInsuranceAgentConfiguration] set Keyword2=PC.ServerConfig fr ...

  4. hdu 4828 Grids 卡特兰数+逆元

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Problem D ...

  5. s表达式和json表达式

    s表达式 + 1 2 3普通表达式 1+2+3json表达式{ +:[1, 2, 3]}优点,一个运算符,无限个参数 s表达式 * (+ 1 2) 3普通表达式 1+(2*3)json表达式{ *:[ ...

  6. XAF学习笔记之 Upcasting

    通常,我们会定义继承层次结构,假设有类型,CustomerBase,CustomerTrialed,CustomerRegistered三个类型,并且继承结构如下: 业务对象代码定义如下: using ...

  7. Xstream 学习地址

    http://forestqqqq.iteye.com/category/301129

  8. Javascript屏蔽回车提交表单

    html利用input防止回车提交 默认情况下,单个输入框,无论按钮的type="submit"还是type="button"类型,回车即提交. 1.当type ...

  9. HttpClient的CircularRedirectException异常原因及解决办法

    HttpClient的CircularRedirectException异常原因及解决办法 这两天在使用我自己爬虫抓取网页的时候总是出现 org.apache.http.client.ClientPr ...

  10. spring之aop概念和配置

    面向切面的一些概念: 简单说: 连接点就一些方法,在这些方法基础上需要额外的一些业务需求处理. 切入点就是方法所代表的功能点组合起来的功能需求. 通知就是那些额外的操作. 织入就是使用代理实现整个切入 ...