297. Serialize and Deserialize Binary Tree
来源:互联网 发布:linux kill -9 编辑:程序博客网 时间:2024/05/16 03:06
问题:
用自己的方法把二叉树转化为字符串,在用自己的方法把这个字符串转为二叉树
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
思路:先序遍历输出二叉树,遇到空就输出null
再递归一下来吧字符串转化为树
程序:
class pointer {int pos = 0;} public class Codec {public String post(TreeNode root) {if(root == null) return "null,";else return Integer.toString(root.val)+","+post(root.left)+post(root.right);} // Encodes a tree to a single string. public String serialize(TreeNode root) { return post(root); } public TreeNode decode(String[] str, pointer p) { if(str[p.pos].equals("null")) { p.pos++; return null; } else { TreeNode rt = new TreeNode(Integer.parseInt(str[p.pos])); p.pos++; rt.left = decode(str, p); rt.right = decode(str, p); return rt; } } // Decodes your encoded data to tree. public TreeNode deserialize(String data) { String[] strs = data.split(","); return decode(strs, new pointer()); }}
0 0
- 297. Serialize and Deserialize Binary Tree
- 297. Serialize and Deserialize Binary Tree
- [LeetCode]297. Serialize and Deserialize Binary Tree
- LeetCode 297. Serialize and Deserialize Binary Tree
- leetcode 297. Serialize and Deserialize Binary Tree
- LeetCode 297. Serialize and Deserialize Binary Tree
- 297. Serialize and Deserialize Binary Tree
- 297. Serialize and Deserialize Binary Tree
- 297. Serialize and Deserialize Binary Tree
- Leetcode 297. Serialize and Deserialize Binary Tree
- 297. Serialize and Deserialize Binary Tree
- 297. Serialize and Deserialize Binary Tree
- 297. Serialize and Deserialize Binary Tree
- LeetCode 297. Serialize and Deserialize Binary Tree
- [LeetCode]297. Serialize and Deserialize Binary Tree
- 297. Serialize and Deserialize Binary Tree
- 297. Serialize and Deserialize Binary Tree
- 297. Serialize and Deserialize Binary Tree
- Linux — 系统启动流程
- 初学者必看 RS422串口通信
- android 实现漫天飞舞雪花以及下雨天的效果
- Exception:org.eclipse.m2e.wtp.MarkedException: Unable to configure OHBC
- Android中Relativelayout各个属性
- 297. Serialize and Deserialize Binary Tree
- python爬虫获取google镜像
- Android 6.0 inflate过程分析
- Coroutine,你究竟干了什么?(小续)
- NYOJ 1003 MAX SUM
- 20160128.CCPP体系详解(0007天)
- JavaSE019_反射应用之动态代理
- struts2动态方法调用(DMI)
- Android中自定义一个事件监听器