LeetCode 449. Serialize and Deserialize BST 序列化和反序列化二叉搜索树 (Java)
题目:
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.
The encoded string should be as compact as possible.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
分析:
序列化一颗二叉搜索树,可以和序列化反序列化二叉树用相同的做法,这次就用层次遍历来做。
程序:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec { // Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root == null)
return "";
StringBuilder str = new StringBuilder();
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
LinkedList<TreeNode> temp = new LinkedList<>();
for(int i = 0; i < len; ++i){
TreeNode node = queue.removeFirst();
if (node != null) {
temp.add(node.left);
temp.add(node.right);
str.append(node.val).append(",");
} else {
str.append("#,");
}
}
queue = temp;
//System.out.println(queue.toString());
}
return str.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data == "" || data == null)
return null;
String[] strs = data.split(",");
Queue<String> l = new ArrayDeque<>(Arrays.asList(strs));
Queue<TreeNode> nodes = new ArrayDeque<>();
TreeNode root = new TreeNode(Integer.parseInt(l.poll()));
nodes.offer(root);
while(!nodes.isEmpty() && !l.isEmpty()){
TreeNode node = nodes.poll();
String strl = l.poll();
if(!strl.equals("#")){
node.left = new TreeNode(Integer.parseInt(strl));
nodes.offer(node.left);
}
String strr = l.poll();
if(!strr.equals("#")){
node.right = new TreeNode(Integer.parseInt(strr));
nodes.offer(node.right);
}
}
return root;
} } // Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
LeetCode 449. Serialize and Deserialize BST 序列化和反序列化二叉搜索树 (Java)的更多相关文章
- 449 Serialize and Deserialize BST 序列化和反序列化二叉搜索树
详见:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ C++: /** * Definition fo ...
- [leetcode]449. 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 ...
- Java实现 LeetCode 449 序列化和反序列化二叉搜索树
449. 序列化和反序列化二叉搜索树 序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法 ...
- 【leetcode-449】序列化和反序列化二叉搜索树
序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法来序列化和反序列化二叉搜索树. 对序列 ...
- LeetCode 449. Serialize and Deserialize BST
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
- [leetcode]449. Serialize and Deserialize BST设计BST的编解码
这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以 唯一得确定. ...
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- 【leetcode】449. Serialize and Deserialize BST
题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...
- 449. Serialize and Deserialize BST
https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...
随机推荐
- react 框架(antd)的使用方法
脚手架 安装 npm install -g create-react-app 引入: import React, { Component } from "react"; im ...
- 力扣238(java)-除自身以外数组的乘积(中等)
题目: 给你一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 . 题目数据 保证 数组 nums之中任意元素的全 ...
- 【pytorch学习】之微积分
4 微积分 在2500年前,古希腊人把一个多边形分成三角形,并把它们的面积相加,才找到计算多边形面积的方法.为了求出曲线形状(比如圆)的面积,古希腊人在这样的形状上刻内接多边形.如图所示,内接多边形的 ...
- 揭秘 RocketMQ 新特性以及在金融场景下的实践
2019 年末, RocketMQ 正式发布了 4.6.0 版本,增加了" Request-Reply "的同步调用的新特性." Request-Reply " ...
- 提升Java字符串编码解码性能的技巧
简介:常见的字符串编码有LATIN1.UTF-8.UTF-16.GB18030,他们各有各的特点,且之间的转换比较复杂.本文将为大家介绍提升Java字符串编码解码性能的技巧. 作者 | 温绍锦 (高 ...
- 浅谈 Linux 高负载的系统化分析
简介: 浅谈 Linux 高负载的系统化分析,阿里云系统组工程师杨勇通过对线上各种问题的系统化分析. 讲解 Linux Load 高如何排查的话题属于老生常谈了,但多数文章只是聚焦了几个点,缺少整体 ...
- [TP5] 浅谈 ThinkPHP 的 Hook 行为事件及监听执行
TP5 中使用 \think\Hook::add('xx', '\app\xxx\behavior\Xx') 注册行为. 也可以在 application/tags.php 中统一注册. 在需要监听执 ...
- Mac安装mysql5.7
1.下载文件(访问就直接下载了) http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.10-osx10.10-x86_64.dmg 2.打开下 ...
- SAP Adobe Form 教程二 表
本文将介绍一些进阶内容,前文:SAP Adobe Form 教程一 简单示例 方法和对比 使用表对象(Table Object)创建表 优点: 它简单易行. 当我们只有很少的字段单行时,我们可以使用它 ...
- SqlServer2008R2 在开始菜单中找不到配置管理器
直接找到C:\Windows\SysWOW64\SQLServerManager10.msc,打开即可.