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,先序遍历确实是可以 唯一得确定. ...
随机推荐
- [REFERENCE] Real-Time-Normal-Map-Dxt-Compression
DXT5N & 3Dc(aka BC5) compression in common code & SIMD: http://mrelusive.com/publications/pa ...
- 对话框Dialog
QMainWindow QMainWindow是 Qt 框架带来的一个预定义好的主窗口类. 主窗口,就是一个普通意义上的应用程序(不是指游戏之类的那种)最顶层的窗口.通常是由一个标题栏,一个菜单栏,若 ...
- C# excel操作
开源的Excel操作项目: http://www.cnblogs.com/lwme/archive/2011/11/27/2265323.html 添加引用:Microsoft Excel 11.0 ...
- LA 4287
Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...
- Difference Between Initialization and Assignment in C++
Initialization happens when a variable is given a value at the moment it is created. Assignment obli ...
- [转载] Linux poll机制
原地址:http://hongwazi.blog.163.com/blog/#m=0&t=3&c=poll poll的是一种查询的方式,英文解释 :民意调查 函数原型:int poll ...
- Windows SEH学习 x86
windows 提供的异常处理机制实际上只是一个简单的框架.我们通常所用的异常处理(比如 C++ 的 throw.try.catch)都是编译器在系统提供的异常处理机制上进行加工了的增强版本.这里先抛 ...
- grep sed
grep -q angeltoto "a.txt"&& (sed -i '/angeltoto/c\'"angeltoto=BUPT" &quo ...
- java.lang.ClassCastException: sun.jdbc.odbc.JdbcOdbcStatement cannot be cast to java.beans.Statement
当导入的包为:import java.sql.Statement;时,无任何错误 当导入的包为:import java.beans.Statement;时,出错
- servlet学习笔记一
Servlet一.基本概念 我们的程序根据是否需要访问网络,可分为网络程序和非网络程序.而 网络程序又分为B/S结构和C/S结构. 什么是C/S?即客户端(Client)/服务器(Server)模式. ...