# 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):
def dfs(root):
if root:
left = dfs(root.left)
right = dfs(root.right)
return str(root.val) +","+ left +","+ right
else:
return "None"
return dfs(root)
def deserialize(self, data):
nums = iter(data.split(","))
def dfs(nums):
val = next(nums)
if val == "None":
return None
else:
root = TreeNode(val)
root.left = dfs(nums)
root.right = dfs(nums)
return root
root = dfs(nums)
return root
# Your Codec object will be instantiated and called as such:
# ser = Codec()
# deser = Codec()
# ans = deser.deserialize(ser.serialize(root))
标签:right,return,nums,Python,self,dfs,二叉树,序列化,root
From: https://www.cnblogs.com/DCFV/p/18428727