当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
Centos7.9 install MySQL 5.7 and set startup
POI replacing text and pictures in docx
From 5 seconds to 1 second, the system flies
一线大厂面试官问:你真的懂电商订单开发吗?
面试官:请你介绍一下缓存穿透、缓存空值、缓存雪崩、缓存击穿的,通俗易懂
Yolov6's fast and accurate target detection framework is open source
面试官:用分库分表如何做到永不迁移数据和避免热点问题?
【OpenAirInterface5g】RRC NR解析之RrcSetupComplete
语音信号处理-概念(二):幅度谱(短时傅里叶变换谱/STFT spectrum)、梅尔谱(Mel spectrum)【语音的深度学习主要用幅度谱、梅尔谱】【用librosa或torchaudio提取】
云服务器配置ftp、企业官网、数据库等方法
随机推荐
Guava scheduled task
Coggle 30 Days of ML 7月竞赛学习
面试官:请你介绍一下缓存穿透、缓存空值、缓存雪崩、缓存击穿的,通俗易懂
What is the difference between volatile and synchronized?
One person manages 1000 servers? This automatic operation and maintenance tool must be mastered
The song of cactus -- throwing stones to ask the way (1)
使用 Blackbox Exporter 测试网络连通性
mysql关于自增和不能为空
Hutool symmetric encryption
manim 数学引擎
正斜杠反斜杠的由来
Classical cryptosystem -- substitution and replacement
(resolved) NPM suddenly reports an error cannot find module 'd:\program files\nodejs\node_ modules\npm\bin\npm-cli. js‘
Park and unpark in unsafe
Custom palette for ggplot2
[leetcode] day90 the element with the smallest K in the binary search tree
内存屏障今生之Store Buffer, Invalid Queue
云服务器配置ftp、企业官网、数据库等方法
oracle的similarity方法实现原理
一個人管理1000臺服務器?這款自動化運維工具一定要掌握