当前位置:网站首页>Sword finger offer 07 Rebuild binary tree
Sword finger offer 07 Rebuild binary tree
2022-06-27 07:27:00 【Yake1965】
The finger of the sword Offer 07. Reconstruction of binary tree
recursive
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length == 0) return null;
TreeNode root = new TreeNode(preorder[0]);
// Question 1 : Find the location of the root node , adopt HashMap solve .
int idx = 0;
for(; idx < inorder.length; idx++){
if(inorder[idx] == preorder[0]) break;
}
// Question two :java Array cannot get subarray , Solve by copying or indexing .
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;
}
}
| Root node preamble index | Middle order left boundary | Middle order right boundary | |
|---|---|---|---|
| The left subtree | rootidx + 1 | left | i - 1 |
| Right subtree | rootidx + i - left + 1 | i + 1 | right |
TIPS: i - left + rootidx + 1 It means Root node index + Left subtree length + 1
class Solution {
HashMap<Integer, Integer> d = new HashMap<>(); // Class variables
public TreeNode buildTree(int[] preorder, int[] inorder) {
// In the middle order, the root node divides the left and right subtrees [left, idx), (idx, right)
// It is convenient to obtain the location of the root node
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);
}
// The root node index is at preorder Middle computation , The scope is inorder Middle computation .
TreeNode recur(int[] preorder, int ridx, int left, int right) {
if(left > right) return null; // Recursive termination
TreeNode node = new TreeNode(preorder[ridx]); // Establish root node
int i = d.get(preorder[ridx]); // The root node divides the left and right subtrees
node.left = recur(preorder, ridx + 1, left, i - 1); // Turn on left subtree recursion
node.right = recur(preorder, ridx + i - left + 1, i + 1, right); // Turn on right subtree recursion
return node;
}
}
iteration
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
int n = preorder.length, idx = 0; // inorder Indexes
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++){
// Traverse preorder
int val = preorder[i]; // Antecedent value
TreeNode top = q.peek();
if(top.val != inorder[idx]){
// Construct the left node
top.left = new TreeNode(val);
q.push(top.left);
} else {
while(!q.isEmpty() && q.peek().val == inorder[idx]){
top = q.pop();
idx++; // Traverse inorder
}
top.right = new TreeNode(val); // Construct the right node
q.push(top.right);
}
}
return root;
}
}
边栏推荐
猜你喜欢

Interviewer: do you have any plan to request a lot of data that does not exist in redis to destroy the database?

进程终止(你真的学会递归了吗?考验你的递归基础)

2022 cisp-pte (II) SQL injection

Machine learning

IDEA连接数据库报错

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

Interviewer: you use Lombok every day. What is its principle? I can't answer

Idea one click log generation

Configuring FTP, enterprise official website, database and other methods for ECS

Yarn create vite reports an error 'd:\program' which is neither an internal or external command nor a runnable program or batch file
随机推荐
How to download opencv? How to configure opencv after downloading?
MySQL
仙人掌之歌——投石问路(1)
View functions in tidb
How torch.gather works
SQL 注入绕过(一)
How to implement redis cache of highly paid programmers & interview questions series 116? How do I find a hot key? What are the possible problems with caching?
从5秒优化到1秒,系统飞起来了...
SQL考勤查询间隔一小时
poi导出excle
Centos7.9 install MySQL 5.7 and set startup
1-4 进制表示与转换
JDBC parameterized query example
Date database date strings are converted to and from each other
JDBC reads MySQL data list
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,你有什么方案?
R 语言 基于关联规则与聚类分析的消费行为统计
apifox学习
如何优雅的写 Controller 层代码?
语音信号处理-概念(二):幅度谱(短时傅里叶变换谱/STFT spectrum)、梅尔谱(Mel spectrum)【语音的深度学习主要用幅度谱、梅尔谱】【用librosa或torchaudio提取】