当前位置:网站首页>Force deduction solution summary 449 serialization and deserialization binary search tree
Force deduction solution summary 449 serialization and deserialization binary search tree
2022-06-12 02:08:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
Serialization is the process of converting a data structure or object into a series of bits , So that it can be stored in a file or memory buffer , Or through a network link , To rebuild later in the same or another computer environment .
Design an algorithm to serialize and deserialize Binary search tree . On serialization / There is no limit to how the deserialization algorithm works . You just need to make sure that the binary search tree can be serialized into strings , And the string can be deserialized into the original binary search tree .
The encoded string should be as compact as possible .
Example 1:
Input :root = [2,1,3]
Output :[2,1,3]
Example 2:
Input :root = []
Output :[]
Tips :
The range of nodes in the tree is [0, 104]
0 <= Node.val <= 104
Subject data Guarantee The input tree is a binary search tree .
Pass times 27,875 Submit the number 47,327
source : Power button (LeetCode)
link :https://leetcode.cn/problems/serialize-and-deserialize-bst
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * We can solve the problem according to the concept of sequence traversal . * When serializing , Each time we traverse the current level , Record the next level , At the same time, the string of the next level is generated . If the next level is not all empty , Then add it to the last string . * When deserializing , We also set two list To record , Record the current level and the next level respectively . With "," Split string , Number of iterations per time , Twice as much as the previous level ( Not included as null The situation of ).
Code :
public class Solution449 {
public static class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if (root == null) {
return "";
}
List<TreeNode> treeNodes = new ArrayList<>();
List<TreeNode> nextNodes = new ArrayList<>();
treeNodes.add(root);
StringBuilder builder = new StringBuilder();
builder.append(root.val);
builder.append(",");
StringBuilder lineBuilder = new StringBuilder();
while (treeNodes.size() > 0) {
builder.append(lineBuilder);
lineBuilder.setLength(0);
nextNodes.clear();
for (TreeNode node : treeNodes) {
if (node.left != null) {
lineBuilder.append(node.left.val);
lineBuilder.append(",");
nextNodes.add(node.left);
} else {
lineBuilder.append("#,");
}
if (node.right != null) {
lineBuilder.append(node.right.val);
lineBuilder.append(",");
nextNodes.add(node.right);
} else {
lineBuilder.append("#,");
}
}
treeNodes.clear();
treeNodes.addAll(nextNodes);
}
return builder.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.length() == 0) {
return null;
}
String[] split = data.split(",");
TreeNode root = null;
List<TreeNode> treeNodes = new ArrayList<>();
List<TreeNode> nextNodes = new ArrayList<>();
int base = 1;
int start = 0;
while (start < split.length) {
nextNodes.clear();
for (int i = 0; i < base; i++) {
if (start + i >= split.length) {
break;
}
String value = split[start + i];
if (root == null) {
root = new TreeNode(Integer.parseInt(value));
nextNodes.add(root);
continue;
}
if (value.equals("#")) {
continue;
}
TreeNode currentNode = new TreeNode(Integer.parseInt(value));
TreeNode parent = treeNodes.get(i / 2);
if (i % 2 == 0) {
parent.left = currentNode;
} else {
parent.right = currentNode;
}
nextNodes.add(currentNode);
}
start = start + base;
base = nextNodes.size() * 2;
treeNodes.clear();
treeNodes.addAll(nextNodes);
System.out.print("");
}
return root;
}
}
}边栏推荐
- 通用树形结构的迭代与组合模式实现方案
- Glfwpollevents() program crash
- serialization and deserialization
- 力扣解法汇总883-三维形体投影面积
- BaseDexClassLoader那些事
- Force deduction solution summary 358 Mini parser
- DDD的分层架构
- 力扣解法汇总462-最少移动次数使数组元素相等 II
- php安全开放10文章列表模块的分页编写
- In Net platform using reflectiondynamicobject to optimize reflection calling code
猜你喜欢

如何最大化的利用各种匹配方式? ——Google SEM

How to stop anti-virus software from blocking a web page? Take gdata as an example

Add sequence number column to MySQL query result set

Oracle 11g graphic download installation tutorial (step by step)

Spiral matrix (skill)

leetcodeSQL:612. Nearest distance on plane

Ozzanmation action system based on SSE

Bracket generation (backtracking)

Installing MySQL version 5.5 database for Linux (centos6)

How to automatically color cells in Excel
随机推荐
Is the bidding price fixed for each click?
Introduction to SVM
The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries
BaseDexClassLoader那些事
Leetcode 55 jump game
PHP development 09 article module deletion and article classification writing
Xcall cluster script (view JPS command)
如何定位关键词使得广告精准投放。
Modification of system module information of PHP security development 12 blog system
How to improve the advertising rating of advertising, that is, the quality score?
leetcodeSQL:612. Nearest distance on plane
State owned assets into shares, has Jianye real estate stabilized?
力扣解法汇总868-二进制间距
程序员应该如何解决买菜难问题?手把手带你利用无障碍辅助功能快速下单抢菜
“中国东信杯”广西大学第四届程序设计竞赛(同步赛)
Implementation scheme of iteration and combination pattern for general tree structure
Various error reporting solutions encountered by Kali during Empire installation
Invert words in a string (split, double ended queue)
Google Ads 竞价的运作机制
力扣解法汇总417-太平洋大西洋水流问题