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,先序遍历确实是可以 唯一得确定. ...
随机推荐
- 监听HTML input输入框值的即时变化onpropertychange、oninput兼容IE,Chrome,FF,Opera等
转自:http://blog.csdn.net/itchiang/article/details/7769337 要达到的效果 很多情况下我们都会即时监听输入框值的变化,以便作出即时动作去引导浏览者增 ...
- CkEditor 插件开发
CKEditor的插件开发其实很简单只需要两步.1.通过CKEditor.plugins.add()方法编写插件的逻辑主体, 2.告诉CKEditor我们有一个自定义插件需要添加进来. //创建插件逻 ...
- C++ 类的成员函数指针 ( function/bind )
这个概念主要用在C++中去实现"委托"的特性. 但现在C++11 中有了 更好用的function/bind 功能.但对于类的成员函数指针的概念我们还是应该掌握的. 类函数指针 就 ...
- UnityException: Texture is not readable
原地址:http://blog.csdn.net/emoonight/article/details/18002913 fore you can save or load a Texture, you ...
- BZOJ2199: [Usaco2011 Jan]奶牛议会
趁此机会学了一下2-SAT. 以前的2-SAT都是用并查集写的,只能应用于极小的一部分情况,这次学了一正式的2-SAT,是用一张有向图来表示其依赖关系. 2-SAT的介绍参见刘汝佳<训练指南&g ...
- c# 发送消息到Email
/// <summary> /// 发送消息到Email /// </summary> /// <param name=&quo ...
- 就地交叉数组元素[a1a2b1b2]->[a1b1a2b2]
问题描述: If [a1,a2,a3...,an,b1,b2...bn] is given input change this to [a1,b1,a2,b2.....an,bn] , solutio ...
- POJ 2021 Relative Relatives(map+树的遍历)
题意: 今天是Ted的100岁生日.凑巧的是,他家族里面每个人都跟他同一天生日,但是年份不同. 现在只给出一些 父亲的名字,孩子的名字,以及孩子出生时父亲的年龄, 要求将Ted以外的家族成员按年龄降序 ...
- HDU 2126 Buy the souvenirs (01背包,输出方案数)
题意:给出t组数据 每组数据给出n和m,n代表商品个数,m代表你所拥有的钱,然后给出n个商品的价值 问你所能买到的最大件数,和对应的方案数.思路: 如果将物品的价格看做容量,将它的件数1看做价值的话, ...
- awk处理之案例三:awk去掉不需要的文本行
编译环境 本系列文章所提供的算法均在以下环境下编译通过. [脚本编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...