当前位置:网站首页>剑指 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;
}
}
边栏推荐
- 进程终止(你真的学会递归了吗?考验你的递归基础)
- 将通讯录功能设置为数据库维护,增加用户名和密码
- 如何优雅的写 Controller 层代码?
- oracle的similarity方法实现原理
- One person manages 1000 servers? This automatic operation and maintenance tool must be mastered
- Meaning of 0.0.0.0:x
- 从5秒优化到1秒,系统飞起来了...
- SQL考勤查询间隔一小时
- ggplot2的自定义调色板
- MPC control of aircraft wingtip acceleration and control surface
猜你喜欢

The interviewer of a large front-line factory asked: do you really understand e-commerce order development?

使用 Blackbox Exporter 测试网络连通性

How to download opencv? How to configure opencv after downloading?

Park and unpark in unsafe

聊聊领域驱动设计

Rust Async: smol源码分析-Executor篇

从5秒优化到1秒,系统飞起来了...

yarn create vite 报错 ‘D:\Program‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件

【编译原理】山东大学编译原理复习提纲

面试官:用分库分表如何做到永不迁移数据和避免热点问题?
随机推荐
Manim math engine
Tidb database Quick Start Guide
解决 Win10 Wsl2 IP 变化问题
Rust中的Pin详解
攻防演习防御体系构建之第二篇之应对攻击的常用策略
2018 mathematical modeling competition - special clothing design for high temperature operation
ggplot2的自定义调色板
Oppo interview sorting, real eight part essay, abusing the interviewer
VNC Viewer方式的远程连接树莓派
Modeling competition - optical transport network modeling and value evaluation
HTAP Quick Start Guide
云服务器配置ftp、企业官网、数据库等方法
2022 cisp-pte (I) document contains
面试官:用分库分表如何做到永不迁移数据和避免热点问题?
MySQL
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
(已解决) MINet 进行测试时报错如下 raise NotImplementedError
程序人生 - 程序员三十五岁瓶颈你怎么看?
MySQL
进程终止(你真的学会递归了吗?考验你的递归基础)