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,先序遍历确实是可以 唯一得确定. ...
随机推荐
- JS 数组的基础知识
数组 一.定义 1.数组的文字定义 广义上说,数组是相同类型数据的集合.但是对于强类型语言和弱类型语言来说其特点是不一样的.强类型语言数组和集合有以下特点. 数组强类型语言:1.数组里面只能存放相同数 ...
- redis cluster安装部署(测试环境)
redis 应用于web前端,做缓存和数据存取的速度是挺可观的,最近看了一些资料,手痒了,就弄了一个测试环境,两台方案,试用一下. ##Redis 集群部署## 一,方案调研: 参考博客: http: ...
- 还原TexturePacker plist 文件以及图片的方法 (切开各小图片)
原地址:http://blog.csdn.net/linuxchen/article/details/16865645 Python 脚本:(来自网络) unpack_plist.py 命令行: py ...
- 鼠标滚轮事件MouseWheel
其实在大多数浏览器(IE6, IE7, IE8, Opera 10+, Safari 5+,Chrome)中,都提供了 "mousewheel" 事件.但杯具的是 Firefox ...
- HDU 1316 How Many Fibs?(java,简单题,大数)
题目 /** * compareTo:根据该数值是小于.等于.或大于 val 返回 -1.0 或 1: public int compareTo(BigInteger val) 将此 BigInteg ...
- 替代jquery
如果不需要过多操作,不引用jquery 1.document.ready :$(function(){}) http://www.cnblogs.com/a546558309/p/3478344.ht ...
- 二叉查找树的查找、插入和删除 - Java实现
http://www.cnblogs.com/yangecnu/p/Introduce-Binary-Search-Tree.html 作者: yangecnu(yangecnu's Blog on ...
- 妙味课堂——HTML+CSS(第四课)(一)
这一课学的东西真是太多了,还不赶快记下来,留待以后慢慢回味! 首先我们回顾一下inline-block的特性: 使块元素在一行显示 使内嵌支持宽高 换行被解析了(问题) 不设置宽度的时候,宽度由内容撑 ...
- 由枚举模块到ring0内存结构 (分析NtQueryVirtualMemory)
是由获得进程模块而引发的一系列的问题,首先,在ring3层下枚举进程模块有ToolHelp,Psapi,还可以通过在ntdll中获得ZwQuerySystemInformation的函数地址来枚举,其 ...
- C# DES 加密 解密
//注意:密钥必须为8位 private const string m_strEncryptKey = "abcd1234"; /// <summary> /// 加密 ...