-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path序列化二叉树.py
62 lines (51 loc) · 1.66 KB
/
序列化二叉树.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
'''
请实现两个函数,分别用来序列化和反序列化二叉树
'''
# 前序遍历 + 分治递归
# 第一种实现
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def Serialize(self, root):
# write code here
if not root: return "$"
return str(root.val) + "," + self.Serialize(root.left) + "," + self.Serialize(root.right)
def Deserialize(self, s):
root, index = self.deserialize(s.split(","), 0)
return root
def deserialize(self,s,index):
if s[index]=='$':
return None,index+1
root=TreeNode(int(s[index]))
index += 1
root.left, index = self.deserialize(s, index)
root.right, index = self.deserialize(s, index)
return root, index
# 第二种实现
# 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):
return str(root.val) + "," + self.serialize(root.left) + ',' + self.serialize(root.right) if root else "$"
def deserialize(self, data):
self.index = -1
data = data.split(',')
def dfs(data):
self.index += 1
if data[self.index] == '$':
return None
root = TreeNode(int(data[self.index]))
root.left = dfs(data)
root.right = dfs(data)
return root
return dfs(data)
# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))