当前位置:网站首页>剑指 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;
}
}
边栏推荐
- tracepoint
- oracle的similarity方法实现原理
- Installation and functions of uview
- Some settings about postfix completion code template in idea
- 2022 le fichier CISP - Pte (i) contient:
- 2022 cisp-pte (I) document contains
- YOLOv6又快又准的目标检测框架 已开源
- Difference between boundvalueops and opsforvalue
- multiprocessing. Detailed explanation of pool
- Hutool symmetric encryption
猜你喜欢

Oppo interview sorting, real eight part essay, abusing the interviewer
[email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT"/>NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT

小米面试官:听你说精通注册中心,我们来聊 3 天 3 夜

2022 CISP-PTE(一)文件包含

Process termination (have you really learned recursion? Test your recursion Foundation)

再见了,敏捷Scrum

Meaning of 0.0.0.0:x

Mathematical modeling contest for graduate students - optimal application of UAV in rescue and disaster relief

一个人管理1000台服务器?这款自动化运维工具一定要掌握

The interviewer of a large front-line factory asked: do you really understand e-commerce order development?
随机推荐
Guava tutorial collect some cases and write Google tool classes slowly
Visual Studio VS 快捷键使用大全
Optimistic and pessimistic affairs
(已解决) npm突然报错 Cannot find module ‘D:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js‘
攻防演习防御体系构建之第二篇之应对攻击的常用策略
YOLOv6又快又准的目标检测框架 已开源
OpenCV怎么下载?OpenCV下载后怎么配置?
Classical cryptosystem -- substitution and replacement
Restrictions on the use of tidb
SQL 注入绕过(一)
Some settings about postfix completion code template in idea
从5秒优化到1秒,系统飞起来了...
Self test in the second week of major 4
Manim math engine
Unrecognized VM option ‘‘
2022 CISP-PTE(二)SQL注入
vs怎么配置OpenCV?2022vs配置OpenCV详解(多图)
SQL injection bypass (I)
Bean拷贝详解
2022 CISP-PTE(一)文件包含