当前位置:网站首页>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);
}
边栏推荐
- 基于Arduino和ESP8266的连接手机热点实验(成功)
- Map and set
- Simple use of drools decision table
- Pytorch builds LSTM to realize clothing classification (fashionmnist)
- Leetcode739 daily temperature
- 史上最易懂的f-string教程,收藏這一篇就够了
- jenkins 凭证管理
- Brush questions --- binary tree --2
- 上传文件时,服务器报错:IOFileUploadException: Processing of multipart/form-data request failed. 设备上没有空间
- MySQL and PostgreSQL methods to grab slow SQL
猜你喜欢
SparkContext: Error initializing SparkContext解决方法
记录一下MySql update会锁定哪些范围的数据
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
China traffic sign detection data set
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
arcgis js 4.x 地图中加入图片
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
随机推荐
drools动态增加、修改、删除规则
Take you ten days to easily finish the finale of go micro services (distributed transactions)
Intel 内部指令 --- AVX和AVX2学习笔记
Go学习笔记—基于Go的进程间通信
MySQL与PostgreSQL抓取慢sql的方法
Brush questions --- binary tree --2
drools执行完某个规则后终止别的规则执行
How does Premiere (PR) import the preset mogrt template?
drools决策表的简单使用
Sub thread get request
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
Jenkins用户权限管理
分布式机器学习框架与高维实时推荐系统
【C语言】十进制数转换成二进制数
Go learning notes - multithreading
(C language) octal conversion decimal
Simple understanding of ThreadLocal
Leetcode14 最长公共前缀
Writing method of then part in drools
怎样写一篇赏心悦目的英文数学论文