Binary Tree Serialization
来源:互联网 发布:富士 打印软件 编辑:程序博客网 时间:2024/06/08 16:36
http://www.lintcode.com/en/problem/binary-tree-serialization/
题目:将二叉树序列化,将序列化的二叉树还原;
解答:按照二叉树序列化规则,如:
3 / \9 20 / \ 15 7
序列化后为:{3,9,20,#,#,15,7}
需注意大括号、逗号
第一次犯错:sb.toString() 后面的括号忘记了;
第二次犯错:忘记考虑“{}”、“,”
第三次犯错:在解序列时,一位以上的数被拆分成多个数字,如“121”被拆分成“1”,“2”,“1”;
优化解答:使用
String[] vals = data.substring(1, data.length() - 1).split(",");
以避免一位以上数字被拆分成多个数字,并且无需考虑遇到“,”的情况,使得程序更加简单。
split(”,“)表示按照”,“为分割符,将字符串转化成字符数组。如:[2:3:4:5].split(":") 结果为:["2", "3", "4", "5"]
代码:
public String serialize(TreeNode root) {
// write your code here
StringBuilder sb = new StringBuilder();
sb.append("{");
if (root == null) {
sb.append("}");
return sb.toString();
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
sb.append(root.val);
while (!queue.isEmpty()) {
TreeNode father = queue.poll();
if (father.left != null) {
sb.append(",");
sb.append(father.left.val);
queue.offer(father.left);
} else {
sb.append(",#");
}
if (father.right != null) {
sb.append(",");
sb.append(father.right.val);
queue.offer(father.right);
} else {
sb.append(",#");
}
}
sb.append("}");
return sb.toString();
}
public TreeNode deserialize(String data) {
// write your code here
if (data.equals("{}")) {
return null;
}
char[] temp = data.toCharArray();
int len = temp.length;
TreeNode root = new TreeNode(temp[1] - '0');
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int i = 3;
while (!queue.isEmpty()) {
TreeNode father = queue.poll();
if (i < len - 1 && temp[i] != '#') {
int num = temp[i] - '0';
while (temp[i + 1] != ',' && temp[i + 1] != '}') {
num = num * 10 + (temp[++i] - '0');
}
TreeNode left = new TreeNode(num);
queue.offer(left);
father.left = left;
i += 2;
} else {
i += 2;
}
if (i < len - 1 && temp[i] != '#') {
int num = temp[i] - '0';
while (temp[i + 1] != ',' && temp[i + 1] != '}') {
num = num * 10 + (temp[++i] - '0');
}
TreeNode right = new TreeNode(num);
queue.offer(right);
father.right = right;
i += 2;
} else {
i += 2;
}
}
return root;
}
- lintcode:Binary Tree Serialization
- Lintcode_7 Binary Tree Serialization
- [Lintcode]Binary Tree Serialization
- Binary Tree Serialization
- Binary Tree Serialization
- Binary Tree Serialization
- LintCode 7:Binary Tree Serialization
- Serialization/Deserialization of a Binary Tree
- [Random Coding] De/Serialization Binary Tree
- Lintcode - Serialization and Deserialization Of Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- iOS xcode 代码行数统计
- 条件随机场总结
- 安卓直播界面开发,自定义照相机拍照,前后摄像头切换教程加源码!
- Wireless Network(POJ
- [PKU暑课笔记] 趁机膜一发线段树和树状数组
- Binary Tree Serialization
- C# AES 加密解密
- linux python2.6 Redis报错
- Restore IP Addresses问题及解法
- fullcalendar日历控件知识点集合
- 在vbox安装Kali 2017.1
- python 截取文件和文件名
- Java8系列--Java Stream进阶篇(收集器简介)
- ESP8266http工作队列的实现方法