-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0449.serialize-and-deserialize-bst_compact_ver.py
66 lines (55 loc) · 1.76 KB
/
0449.serialize-and-deserialize-bst_compact_ver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from collections import deque
# 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 'None'
s = [root]
res = ''
while s:
node = s.pop()
res += str(node.val) + ','
if node.right:
s.append(node.right)
if node.left:
s.append(node.left)
return res[:-1]
# BST前序遍历,第一个是root,后面连续比root value小的是左子树的value,剩下的是有子树的value
def deserialize(self, data):
"""Decodes your encoded data to tree.
:type data: str
:rtype: TreeNode
"""
if data == 'None':
return None
q = deque(data.split(','))
return self.buildTree(q)
# q : deque of preOrder, return treeNode
def buildTree(self, q):
# def printQ(q):
# ans = []
# for val in q:
# ans.append(val)
# print(ans)
# printQ(q)
if len(q) == 0:
return None
root = TreeNode(int(q.popleft()))
leftQue = deque()
while len(q)>0 and int(q[0]) < root.val:
leftQue.append(q.popleft())
root.left = self.buildTree(leftQue)
root.right = self.buildTree(q)
return root
# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))