serialize-and-deserialize-bst
https://leetcode.com/problems/serialize-and-deserialize-bst/
1. 用到Java Queue接口,
// LinkedList实现了Queue接口, ArrayList没有实现
2. 用的Java String.split 函数得到 String数组。
3. 另外一个bug是因为String比较用的 == ,没有用 equals
package com.company;
import apple.laf.JRSUIUtils;
import java.util.*;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
if (root == null) {
return "";
}
// LinkedList实现了Queue接口, ArrayList没有实现
Queue<TreeNode> qe= new LinkedList<>();
qe.offer(root);
sb.append(root.val+",");
while (!qe.isEmpty()) {
TreeNode tn = qe.poll();
if (tn.left != null) {
sb.append(tn.left.val+",");
qe.offer(tn.left);
}
else {
sb.append(",");
}
if (tn.right != null) {
sb.append(tn.right.val+",");
qe.offer(tn.right);
}
else {
sb.append(",");
}
}
return sb.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.equals("")) {
return null;
}
String[] strs = data.split(",");
Queue<TreeNode> qe = new LinkedList<>();
if (strs.length < 1 || strs[0].equals("")) {
return null;
}
TreeNode root = new TreeNode(Integer.valueOf(strs[0]));
qe.offer(root);
int i = 1;
while (!qe.isEmpty()) {
TreeNode tn = qe.poll();
if (strs.length > i && !strs[i].equals("")) {
TreeNode left = new TreeNode(Integer.valueOf(strs[i]));
tn.left = left;
qe.offer(left);
}
i++;
if (strs.length > i && !strs[i].equals("")) {
TreeNode right = new TreeNode(Integer.valueOf(strs[i]));
tn.right = right;
qe.offer(right);
}
i++;
}
return root;
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println("Hello!");
//Solution solution = new Solution();
// Your Codec object will be instantiated and called as such:
TreeNode tn = new TreeNode(2);
TreeNode tn1 = new TreeNode(1);
//TreeNode tn2 = new TreeNode(3);
tn.left = tn1;
//tn.right = tn2;
Codec codec = new Codec();
String code = codec.serialize(tn);
System.out.printf("code:%s\n", code);
TreeNode ret = codec.deserialize(code);
System.out.printf("root:%d\n", ret.val);
System.out.println();
}
}
serialize-and-deserialize-bst的更多相关文章
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- Leetcode: Serialize and Deserialize BST
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [leetcode]449. Serialize and Deserialize BST序列化与反序列化BST
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 449. Serialize and Deserialize BST
https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...
- 449. Serialize and Deserialize BST——几乎所有树的面试题目都会回到BFS或者DFS,使用BFS,None节点存#
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode 449. Serialize and Deserialize BST
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
- 【leetcode】449. Serialize and Deserialize BST
题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...
- [leetcode]449. Serialize and Deserialize BST设计BST的编解码
这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以 唯一得确定. ...
随机推荐
- sample a texture as a rendertarget
ID3D11DeviceContext::PSSetShaderResources: Resource being set to PS shader resource slot 0 is still ...
- HDAO one error
对normal target设置的background clearcolor 导致 远处天空 通过了 normalRejectTest 所以要对normal target单独设置 不能通过test的 ...
- Win32 Plus Extra Height of Caption Bar
you set the size of the non-client area by handling the WM_NCCALCSIZE message. But don't do this unl ...
- O2O模式成功案例分享 汲取精华化为己用
本文通过分享一些公司的o2o成功案例让您了解什么是O2O,o2o的优势,o2o模式有哪些,未来我们要如何做o2o才更有竞争力,学牛人的o2o创新玩法,摸索适合自己的o2o思路.拥抱o2o - 传统企业 ...
- MariaDB Galera Cluster 部署(如何快速部署 MariaDB 集群)
MariaDB Galera Cluster 部署(如何快速部署 MariaDB 集群) OneAPM蓝海讯通7月3日 发布 推荐 4 推荐 收藏 14 收藏,1.1k 浏览 MariaDB 作为 ...
- (转)Learning to Rank for IR的评价指标—MAP,NDCG,MRR
转自:http://www.cnblogs.com/eyeszjwang/articles/2368087.html MAP(Mean Average Precision):单个主题的平均准确率是每篇 ...
- nginx规则和ci的支持
CI框架下nginx重写规则,不再404 http://blog.csdn.net/EI__Nino/article/details/8599304 server { listen 80; serve ...
- 关于解压覆盖IIS文件后,新的文件不具备权限导致DMS系统无法正常运行
向DMS的服务器端站点bin目录覆盖任何补丁文件,请注意:Web站点的bin目录中的文件,IIS的服务进程(Windows2003以上,都是对应Network Services账户)必须对这些文件具 ...
- Jmeter 快速入门教程(三-3) -- 使用参数化
参数化:简单的来理解一下,我们录制了一个脚本,这个脚本中有登录操作,需要输入用户名和密码,假如系统不允许相同的用户名和密码同时登录,或者想更好的模拟多个用户来登录系统. 这个时候就需要对用户名和密码进 ...
- eclipse导入的工程前面有感叹号是什么意思
1.尤其是从其他地方拷贝来并且直接加载的工程,刚打开往往会看到工程的图标上有个红色的感叹号,这是因为build path 出错了,里面有缺失或者无法找到的包. 2. 原因:显示红色感叹号是因为jar包 ...