使用任意方法序列化一个二叉树。

https://leetcode.com/problems/serialize-and-deserialize-binary-tree/

原来带有None作为结束标志的x序遍历的结果是可以唯一的恢复一颗二叉树的!!!

如果没有None结束标记,是无法做到的!

代码没法子讲,自己看吧。

如何快速打印?先print(serialize),再print(serialize(deserialize(serialize(root))))

 1 class Codec:
 2
 3     def serialize(self, root):
 4         """Encodes a tree to a single string.
 5
 6         :type root: TreeNode
 7         :rtype: str
 8         """
 9         def rec(root):
10             if root==None:
11                 return 'None,'
12             return str(root.val)+','+rec(root.left)+rec(root.right)
13         return rec(root)
14
15     def deserialize(self, data):
16         """Decodes your encoded data to tree.
17
18         :type data: str
19         :rtype: TreeNode
20         """
21         def rec(start):
22             i=start
23             while data[i]!=',':
24                 i+=1
25             if i-start==4 and data[start:i]=='None':
26                 return None,start+5
27             # print(data[start:i],'to trans')
28             node=TreeNode(int(data[start:i]))
29             l,ls=rec(i+1)
30             r,rs=rec(ls)
31             node.left=l
32             node.right=r
33             return node,rs
34         return rec(0)[0]

转载于:https://www.cnblogs.com/waldenlake/p/10650710.html

lc 297. Serialize and Deserialize Binary Tree相关推荐

  1. 297. Serialize and Deserialize Binary Tree

    Title 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据. 请设计一个算 ...

  2. LeetCode 297 Serialize and Deserialize Binary Tree

    题目描述 Serialization is the process of converting a data structure or object into a sequence of bits s ...

  3. [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  4. LC 104. Maximum Depth of Binary Tree

    1.题意 104. Maximum Depth of Binary Tree Easy 98540 Given a binary tree, find its maximum depth. The m ...

  5. leetcode 449. Serialize and Deserialize BST | 449. 序列化和反序列化二叉搜索树(BST后序遍历性质)

    题目 https://leetcode.com/problems/serialize-and-deserialize-bst/ 题解 本题的难点在于 利用 BST 的性质. 几个提示 根据后序遍历BS ...

  6. 由任意二叉树的前序遍历序列和中序遍历序列求二叉树的思想方法_算法与数据结构基础 - 二叉树(Binary Tree)...

    二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...

  7. 序列化和反序列化二叉搜索树 Serialize and Deserialize BST

    2019独角兽企业重金招聘Python工程师标准>>> 问题: Serialization is the process of converting a data structure ...

  8. Construct Maximum Binary Tree

    构建一颗「最大树」. 注意consruct的时候最后的return root; 我参考了serialize and deserialize binary tree的build tree 的过程. 这周 ...

  9. Binary Tree Upside Down LC解题记录

    题目内容 Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node ...

最新文章

  1. Canonical面向Ubuntu 16.04 LTS发布首批内核安全修复补丁
  2. webstrom常用键
  3. 网站外链如何发布才能更快的得到高排名呢?
  4. 深度学习笔记:windows+tensorflow 指定GPU占用内存(解决gpu爆炸问题)
  5. POJ 2054 Color a Tree (贪心)
  6. camel.js_Camel 2.11 –具有URL重写功能的HTTP代理路由
  7. 转:html id与name区别
  8. Windows Live Writer 测试
  9. 使用Vitamio打造自己的Android万能播放器(6)——在线播放(播放列表)
  10. row_number()over函数的使用(转)
  11. 镁光ssd管理工具 linux,镁光C400固态硬盘08TH固件及升级软件
  12. 硅谷新传奇Kevin Systrom
  13. 单片机测试开发板用什么软件,怎么知道单片机开发板的好坏
  14. bzoj4565 [Haoi2016]字符合并 (区间DP + 状压DP)
  15. 电商后台设计详细讲解
  16. 常用类(API)第一节
  17. 可以边玩游戏边学编程的手游盘点
  18. centos6.5 ifconfig 显示为eth2,配置文件只有eth0
  19. 虚拟化技术 、 Win系统安装
  20. C++Primer 第10章lambda表达式

热门文章

  1. unity ab包加载_Unity资源管理和打包
  2. Ubuntu下华为方舟编译器环境安装
  3. 如何优雅地停止Java进程
  4. 将JSON对象中的某个字段进行分组和排序(java实现)
  5. mysql linux root密码忘记了怎么办,linux下忘记mysql的root密码解决办法 | 严佳冬
  6. php获取li或者a标签中间的文字
  7. 记录一次laravel5.5的安装
  8. 斯特林数-斯特林反演
  9. [转] CPU GPU TPU
  10. MySQL 加锁处理分析(二)