当前位置:网站首页>剑指 Offer 07. 重建二叉树
剑指 Offer 07. 重建二叉树
2022-06-27 07:05:00 【Yake1965】
剑指 Offer 07. 重建二叉树
递归
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length == 0) return null;
TreeNode root = new TreeNode(preorder[0]);
// 问题一:找根结点的位置,通过 HashMap 解决。
int idx = 0;
for(; idx < inorder.length; idx++){
if(inorder[idx] == preorder[0]) break;
}
// 问题二:java 数组不能获取子数组,通过拷贝或索引解决。
if(idx >= 0){
root.left = this.buildTree(Arrays.copyOfRange(preorder, 1, idx + 1), Arrays.copyOfRange(inorder, 0, idx));
}
if(idx <= preorder.length - 1){
root.right = this.buildTree(Arrays.copyOfRange(preorder, idx + 1, preorder.length), Arrays.copyOfRange(inorder, idx + 1, inorder.length));
}
return root;
}
}
| 根节点前序索引 | 中序左边界 | 中序右边界 | |
|---|---|---|---|
| 左子树 | rootidx + 1 | left | i - 1 |
| 右子树 | rootidx + i - left + 1 | i + 1 | right |
TIPS: i - left + rootidx + 1 含义为 根节点索引 + 左子树长度 + 1
class Solution {
HashMap<Integer, Integer> d = new HashMap<>(); // 类变量
public TreeNode buildTree(int[] preorder, int[] inorder) {
// 中序中根结点分割左右子树 [left, idx), (idx, right)
// 方便获取根结点的位置
for(int i = 0; i < inorder.length; i++)
d.put(inorder[i], i);
// rootidx = 0, left = 0, right = inorder.length - 1
return recur(preorder, 0, 0, inorder.length - 1);
}
// 根结点索引在 preorder 中计算,范围在 inorder 中计算。
TreeNode recur(int[] preorder, int ridx, int left, int right) {
if(left > right) return null; // 递归终止
TreeNode node = new TreeNode(preorder[ridx]); // 建立根节点
int i = d.get(preorder[ridx]); // 根节点划分左右子树
node.left = recur(preorder, ridx + 1, left, i - 1); // 开启左子树递归
node.right = recur(preorder, ridx + i - left + 1, i + 1, right); // 开启右子树递归
return node;
}
}
迭代
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
int n = preorder.length, idx = 0; // inorder 索引
if(preorder == null || n == 0) return null;
TreeNode root = new TreeNode(preorder[0]);
Deque<TreeNode> q = new ArrayDeque<>();
q.push(root);
for(int i = 1; i < n; i++){
// 遍历 preorder
int val = preorder[i]; // 前序值
TreeNode top = q.peek();
if(top.val != inorder[idx]){
// 构造左结点
top.left = new TreeNode(val);
q.push(top.left);
} else {
while(!q.isEmpty() && q.peek().val == inorder[idx]){
top = q.pop();
idx++; // 遍历 inorder
}
top.right = new TreeNode(val); // 构造右结点
q.push(top.right);
}
}
return root;
}
}
边栏推荐
- The song of cactus -- throwing stones to ask the way (1)
- IDEA连接数据库报错
- win10远程连接云服务器
- MySQL
- 请问网页按钮怎么绑定sql语句呀
- Xiaomi Interviewer: let's talk about the proficient Registration Center for three days and three nights
- 语音信号特征提取流程:输入语音信号-分帧、预加重、加窗、FFT->STFT谱(包括幅度、相位)-对复数取平方值->幅度谱-Mel滤波->梅尔谱-取对数->对数梅尔谱-DCT->FBank->MFCC
- Modeling competition - optical transport network modeling and value evaluation
- (resolved) NPM suddenly reports an error cannot find module 'd:\program files\nodejs\node_ modules\npm\bin\npm-cli. js‘
- 从5秒优化到1秒,系统飞起来了...
猜你喜欢

Origin of forward slash and backslash

语音信号特征提取流程:输入语音信号-分帧、预加重、加窗、FFT->STFT谱(包括幅度、相位)-对复数取平方值->幅度谱-Mel滤波->梅尔谱-取对数->对数梅尔谱-DCT->FBank->MFCC

Interviewer: please introduce cache penetration, cache null value, cache avalanche and cache breakdown, which are easy to understand

在线文本数字识别列表求和工具

Park and unpark in unsafe

【毕业季】毕业是人生旅途的新开始,你准备好了吗

面试官:大量请求 Redis 不存在的数据,从而打倒数据库,你有什么方案?

POI replacing text and pictures in docx

一個人管理1000臺服務器?這款自動化運維工具一定要掌握

yarn create vite 报错 ‘D:\Program‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件
随机推荐
Origin of forward slash and backslash
Visual studio vs shortcut key usage
window右键管理
The song of cactus -- throwing stones to ask the way (1)
在线文本数字识别列表求和工具
uview的安装和功能
Oppo interview sorting, real eight part essay, abusing the interviewer
2022 CISP-PTE(一)文件包含
[Kevin's third play in a row] is rust really slower than C? Further analyze queen micro assessment
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,你有什么方案?
manim 数学引擎
Yarn create vite reports an error 'd:\program' which is neither an internal or external command nor a runnable program or batch file
Installation and functions of uview
Machine learning
解决 Win10 Wsl2 IP 变化问题
MPC control of aircraft wingtip acceleration and control surface
程序人生 - 程序员三十五岁瓶颈你怎么看?
攻防演习防御体系构建之第二篇之应对攻击的常用策略
【毕业季】毕业是人生旅途的新开始,你准备好了吗
云服务器配置ftp、企业官网、数据库等方法