Python:序列化二叉树
来源:互联网 发布:浙江省纺织品出口数据 编辑:程序博客网 时间:2024/06/05 17:06
牛客网上的剑指 offer的在线编程:
题目描述
请实现两个函数,分别用来序列化和反序列化二叉树
class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = Noneclass Solution: def Serialize(self, root): # write code here tree, ch = [root], [] while len(tree) > 0: temp = tree.pop(0) if temp is None: ch.append('#*') else: ch.append(str(temp.val) + '*') tree.append(temp.left) tree.append(temp.right) return ''.join(ch) def Deserialize(self, s): # write code here s1, i = s.split('*'), 0 if s1[i] == '#': return None root = TreeNode(int(s1[i])) tree = [root] while len(tree) > 0: te = tree.pop(0) i += 1 if s1[i] != '#': k = TreeNode(int(s1[i])) te.left = k tree.append(k) i += 1 if s1[i] != '#': k = TreeNode(int(s1[i])) te.right = k tree.append(k) return root
阅读全文
0 0
- Python:序列化二叉树
- 二叉树序列化
- 二叉树序列化
- 序列化二叉树
- 序列化二叉树
- 序列化二叉树
- 序列化二叉树
- 序列化二叉树
- 序列化二叉树
- 序列化二叉树
- 序列化二叉树
- 序列化二叉树
- 序列化二叉树
- 序列化二叉树
- 二叉树序列化
- 序列化二叉树
- 序列化二叉树
- 序列化二叉树
- 高性能网络编程
- Codeforces
- java基础易错题
- 深入理解 Android 之 View 的绘制流程(一)
- Python联系笔记2[列表,元组,集合,字典,函数,文件操作,模块,类]
- Python:序列化二叉树
- KS109的STM32的移植
- Codeforces
- 单点LDAP安装配置
- 使用JVisualVM远程监控Tomcat
- 解决The current branch is not configured for pull No value for key branch.master.merge found in config
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) D. Best Edge Weight(最小生成树+LCA+树链剖分)
- Python-matplotlib基本操作
- 冒泡排序