当前位置:网站首页>LeetCode—剑指 Offer 37、38
LeetCode—剑指 Offer 37、38
2022-07-02 09:43:00 【Ostrich5yw】
剑指 Offer 37. 序列化二叉树、38. 字符串的排列
题目描述:[37]
请实现两个函数,分别用来序列化和反序列化二叉树。
你需要设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。
提示:输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。[38]
输入一个字符串,打印出该字符串中字符的所有排列。
你可以以任意顺序返回这个字符串数组,但里面不能有重复元素。
考察重点:
第37题深度遍历实现树–>字符串,如何生成一棵树(采用深度遍历,每一步新建root,并通过返回值给root.Left和root.Right赋值)
第38题先为字符串排序,再按照从前至后进行递归遍历。
第37题
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;
}
}
第38题
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);
}
边栏推荐
- Applet link generation
- K-Means Clustering Visualization in R: Step By Step Guide
- HR wonderful dividing line
- Log4j2
- Natural language processing series (III) -- LSTM
- Leetcode topic [array] -540- single element in an ordered array
- 5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
- JZ63 股票的最大利润
- 刷题---二叉树--2
- H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
猜你喜欢
【工控老马】西门子PLC Siemens PLC TCP协议详解
ES集群中节点与分片的区别
YYGH-BUG-05
Read the Flink source code and join Alibaba cloud Flink group..
Yygh-9-make an appointment to place an order
Jenkins user rights management
SVO2系列之深度濾波DepthFilter
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
使用Sqoop把ADS层数据导出到MySQL
自然语言处理系列(二)——使用RNN搭建字符级语言模型
随机推荐
Mysql database foundation
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
自然语言处理系列(一)——RNN基础
How to Easily Create Barplots with Error Bars in R
寻找二叉树中任意两个数的公共祖先
CDA data analysis -- common knowledge points induction of Excel data processing
SCM power supply
SSH automatically disconnects (pretends to be dead) after a period of no operation
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
MySQL indexes and transactions
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
Leetcode209 长度最小的子数组
GGPlot Examples Best Reference
自然语言处理系列(三)——LSTM
测试左移和右移
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
SparkContext: Error initializing SparkContext解决方法
Lombok common annotations
史上最易懂的f-string教程,收藏這一篇就够了
ThreadLocal的简单理解