AcWing 50. 序列化二叉树
原题链接
困难
作者:
月涌江流
,
2019-03-22 14:44:31
,
所有人可见
,
阅读 1264
Python代码
class Solution:
def serialize(self, root):
if not root:
return "None"
return str(root.val) + ',' + self.serialize(root.left) + ',' + self.serialize(root.right)
def deserialize(self, data):
self.i = -1
data = data.split(',')
return self.dfs(data)
def dfs(self,data):
if self.i >= len(data):
return
self.i += 1
root = None
if data[self.i] != "None":
root = TreeNode(int(data[self.i]))
root.left = self.dfs(data)
root.right = self.dfs(data)
return root