当前位置:网站首页>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);
}
边栏推荐
猜你喜欢

arcgis js 4.x 地图中加入图片
![[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol](/img/13/9002244555ebe8a61660c2506993fa.png)
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol

drools决策表的简单使用

Map和Set

WSL 2 will not be installed yet? It's enough to read this article

Sparkcontext: error initializing sparkcontext solution

drools中then部分的写法

Simple use of drools decision table

甜心教主:王心凌

PyTorch nn. Full analysis of RNN parameters
随机推荐
Codeforces 771 div2 B (no one FST, refers to himself)
arcgis js 4. Add pictures to x map
MySQL and PostgreSQL methods to grab slow SQL
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
Go learning notes - go based interprocess communication
单指令多数据SIMD的SSE/AVX指令集和API
中国交通标志检测数据集
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
堆(優先級隊列)
Simple use of drools decision table
Lombok common annotations
Is the neural network (pinn) with embedded physical knowledge a pit?
mysql索引和事务
Maximum profit of jz63 shares
Drools dynamically add, modify, and delete rules
Go learning notes - multithreading
Sort---
How does Premiere (PR) import the preset mogrt template?
怎样写一篇赏心悦目的英文数学论文
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节