[leetcode]449. Serialize and Deserialize BST序列化与反序列化BST
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.
思路
To makes it the most compact possible, since we just need the order the values were inserted, we do not need to account for null nodes in the string with "#" or "null".
Hence, the final string contains only the values and separators, which makes it the most compact possible.
1. 在encode时候,don't account for null nodes
2. 在decode时候,不再用queue来存整个treenode的信息,而是用大小为1的数组idx来track当前扫到哪一个node了, 用idx来拿到该node value值(即mock up a pass-by-address process)。通过BST的attribute (left < root < right)来build tree.
代码
public class Codec { // Encodes a tree to a single string. public String serialize(TreeNode root) { StringBuilder sb = new StringBuilder(); buildString(root, sb); return sb.toString(); } private void buildString(TreeNode root, StringBuilder sb) { if (root == null) return; sb.append(root.val).append(","); buildString(root.left, sb); buildString(root.right, sb); } // Decodes your encoded data to tree. public TreeNode deserialize(String data) { if(data.length() == 0) return null; // use idx to mockup a pass-by-address int[] idx = new int[]{0}; return buildTree(data.split(","), idx, Integer.MIN_VALUE, Integer.MAX_VALUE); } private TreeNode buildTree(String[] strArr, int[] idx, int min, int max) { if (idx[0] == strArr.length) return null; int val = Integer.parseInt(strArr[idx[0]]); // Go back if out of boundary if (val < min || val > max) return null; TreeNode root = new TreeNode(val); // move to next address idx[0]++; // corresponding to encode pre-order root.left = buildTree(strArr, idx, min, val); root.right = buildTree(strArr, idx, val, max); return root; } }
[leetcode]449. Serialize and Deserialize BST序列化与反序列化BST的更多相关文章
- [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
原题链接在这里: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 ...
- [LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 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 ...
随机推荐
- 飞利浦 PHILIPS 电动牙刷HX6730 拆解
今日,一直比较喜欢用的电动牙刷,飞利浦HX6730坏掉了,初步感觉考虑飞利浦的保修,但是发现发票找不到了.飞利浦的客服也说,电动牙刷的两年保修依据分别是:1.发票开据日期:2.在无发票的情况下,看底部 ...
- Zabbix3.0版Graphtree的安装配置
Graphtrees: https://github.com/OneOaaS/graphtrees 如果是采用yum安装的zabbix-server, 则使用以下方式: # mv /usr/shar ...
- 传输层——UDP报文头介绍
16位源端口 16位目的端口 16位总长度 16位校验和 数据 源端口:长度为16位,2个字节. 目的端口:长度为16位,2个字节. 总长度:长度为16位,2个字节,表示 UDP包头长度 和 数据长度 ...
- ROS:ROS操作类MK.cs
class MK { Stream connection; TcpClient con; public MK(string ip,int port) { con = new TcpClient(); ...
- location search的中文加密
最近项目中遇到一个这样问题,在页面跳转时,追加了location.search,有中文字符,但是在分享第二次时,这个链接无法获取中文字段,变成乱码. 仔细对比,发现在页面分享时,浏览器自动对中文进行了 ...
- cmd从新获取ip
1.ipconfig/release 2.ipconfig/renew
- table tr 加入背景色之后 去掉td之间的空隙
给table加样式 border-collapse:collapse;
- Electrom will-download pause function
1.code from github url-link: https://github.com/electron/electron/issues/7712 // to store downloadIt ...
- 带轮播图、导航栏、商品的简单html,以及轮播图下边数字随轮播图的改变而改变
---恢复内容开始--- 在做这个的时候,最不会的是中间轮播图下边的数字是如何实现转变的,后来加入了jQuery就能实现了. css部分: <style type="text/css& ...
- Global Illumination
[Global Illumination] Global Illumination (GI) is a system that models how light is bounced off of s ...