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的更多相关文章

  1. 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)

    [LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...

  2. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. Leetcode: Serialize and Deserialize BST

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  4. [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 ...

  5. [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. 449. Serialize and Deserialize BST

    https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...

  7. 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 ...

  8. LeetCode 449. Serialize and Deserialize BST

    原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...

  9. 【leetcode】449. Serialize and Deserialize BST

    题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...

  10. [leetcode]449. Serialize and Deserialize BST设计BST的编解码

    这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以 唯一得确定. ...

随机推荐

  1. PHP READ PPT FILE

    function parsePPT($filename) { // This approach uses detection of the string "chr(0f).Hex_value ...

  2. 浅谈javascript中的数据类型和引用类型

    1.概述 javascript中有五种简单数据类型和一种复杂数据类型. 分别是:undefind, null, number, string ,boolean ----简单数据类型          ...

  3. (4)用opengl读入off文件生成可执行文件把模型显示出来(未完待续)

    ·找了好几个程序,好像都达不到我的要求,去教程里看看吧! 在往上抛出了这问题,好几天才有人回答,我已经找到程序了 正好的他的分析对我分解程序很有用 这是一个难度比较高的 首先你要分析.off文件结构, ...

  4. [转载]Jmeter那点事·ForEach和If控制器

    如果我们要实现一个循环,如果城市是北京,则返回首都:否则,返回城市.   一.新建用户自定义变量 添加-配置元件-用户自定义变量, 定义变量注意命名格式:变量名 加 下划线 加 数字(从1开始计数) ...

  5. http://my.oschina.net/u/719192/blog/506062?p={{page}}

    http://my.oschina.net/u/719192/blog/506062?p={{page}}

  6. 李洪强iOS开发之后使用纯代码实现横向滚动的UIScrollView

    李洪强iOS开发之后使用纯代码实现横向滚动的UIScrollView (VTmagic是一个实现左右滚动的控制器的框架,也可以实现此功能) 实现的效果:  01 - 创建四个控制器 02 - 定义需要 ...

  7. Eclipse提示Tomcat miss丢失bug:The Tomcat server configuration at \Servers\Tomcat v5.5 Server at localhost-config is missing.

    Eclipse提示Tomcat miss丢失bug:The Tomcat server configuration at \Servers\Tomcat v5.5 Server at localhos ...

  8. React事件函数简介

    一.事件汇总 二.例子 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset= ...

  9. 编写基于outlook显示的html邮件需要注意的问题

    outlook对于html的支持是有限制的,在编写这些html的时候注意遵循以下规则: Never use colspans or rowspans. Always set correct dimen ...

  10. 物联网操作系统Hello China V1.76(PC串口版)版本发布

    作为向ARM平台移植的基线版本,经过三个多月的努力,Hello China V1.76终于完成并发布.相对原来发布的V1.75版本,该版本主要做了如下修改: 彻底去掉了原来版本源代码中的C++特性,采 ...