当前位置:网站首页>Leetcode - Sword finger offer 37, 38
Leetcode - Sword finger offer 37, 38
2022-07-02 12:21:00 【Ostrich5yw】
The finger of the sword Offer 37. Serialize binary tree 、38. Arrangement of strings
Title Description :[37]
Please implement two functions , Used to serialize and deserialize binary trees .
You need to design an algorithm to realize the serialization and deserialization of binary tree . There's no limit to your sequence / Deserialization algorithm execution logic , You just need to make sure that a binary tree can be serialized into a string and that the string can be deserialized into the original tree structure .
Tips : Input output format and LeetCode The current usage is consistent , Please refer to LeetCode The format of serializing binary tree . You don't have to do this , You can also use other methods to solve this problem .[38]
Enter a string , Print out all the permutations of the characters in the string .
You can return this array of strings in any order , But there's no repetition in it .
Key points of investigation :
The first 37 topic Depth traversal implementation tree –> character string , How to generate a tree ( Using depth traversal , New at each step root, And return the value to root.Left and root.Right assignment )
The first 38 topic Sort the string first , Then recursively traverse from front to back .
The first 37 topic
public class Codec {
public String makeString(TreeNode root){
if(root == null){
return "n";
}
String left = "," + serialize(root.left);
String right = "," + serialize(root.right);
return root.val + left + right;
}
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
String res = makeString(root);
return res;
}
public TreeNode makeTree(ArrayList<String> datas, TreeNode root){
String nowC = datas.get(0);
datas.remove(0);
if(!nowC.equals("n")){
root = new TreeNode(Integer.parseInt(nowC));
root.left = makeTree(datas, root.left);
root.right = makeTree(datas, root.right);
}else{
root = null;
}
return root;
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
ArrayList<String> datas = new ArrayList<>(Arrays.asList(data.split(",")));
if(datas.get(0).equals("n")){
return null;
}
TreeNode root = new TreeNode(Integer.parseInt(datas.get(0)));
datas.remove(0);
root.left = makeTree(datas, root.left);
root.right = makeTree(datas, root.right);
return root;
}
}
The first 38 topic
boolean[] visit;
ArrayList<String> res;
int targetLen;
public int dfs(char[] s, String nowWord){
if(nowWord.length() == targetLen){
res.add(nowWord);
return 0;
}
for(int i = 0;i < targetLen;i ++){
if(visit[i] || (i > 0 && s[i] == s[i-1] && visit[i-1]))
continue;
visit[i] = true;
dfs(s, nowWord + s[i]);
visit[i] = false;
}
return 0;
}
public String[] permutation(String s) {
char[] arr = s.toCharArray();
Arrays.sort(arr);
targetLen = s.length();
visit = new boolean[targetLen];
res = new ArrayList<String>();
dfs(arr, "");
String[] tt = new String[res.size()];
return res.toArray(tt);
}
边栏推荐
- WSL 2 will not be installed yet? It's enough to read this article
- SCM power supply
- Take you ten days to easily finish the finale of go micro services (distributed transactions)
- 深入理解P-R曲线、ROC与AUC
- conda常用命令汇总
- Go学习笔记—基于Go的进程间通信
- 中国交通标志检测数据集
- CDA data analysis -- common knowledge points induction of Excel data processing
- (C语言)八进制转换十进制
- mysql数据库基础
猜你喜欢
Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
CDA data analysis -- Introduction and use of aarrr growth model
【C语言】十进制数转换成二进制数
How does Premiere (PR) import the preset mogrt template?
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
Sparkcontext: error initializing sparkcontext solution
倍增 LCA(最近公共祖先)
arcgis js 4.x 地图中加入图片
Writing method of then part in drools
随机推荐
Sub thread get request
Natural language processing series (I) -- RNN Foundation
PyTorch nn. Full analysis of RNN parameters
单指令多数据SIMD的SSE/AVX指令集和API
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
Go learning notes - multithreading
ES集群中节点与分片的区别
Embedded Software Engineer career planning
MySQL与PostgreSQL抓取慢sql的方法
Writing method of then part in drools
Leetcode122 买卖股票的最佳时机 II
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
Orb-slam2 data sharing and transmission between different threads
Lombok common annotations
drools执行完某个规则后终止别的规则执行
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
Leetcode209 长度最小的子数组
Pytorch builds LSTM to realize clothing classification (fashionmnist)
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
Sort---