当前位置:网站首页>Rebuild binary tree
Rebuild binary tree
2022-06-22 06:42:00 【FIappy Brid】
Title Description

import java.util.*;
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
public class Solution {
// The former sequence traversal : root Left Right
// In the sequence traversal : Left root Right
public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
// Through the preface And middle order traversal Restore a binary tree !
// Recursive version __> Optimize , Is to store the subscript and value in the hash table , It will save more time to find the location !
//return helper(pre,vin,0,pre.length-1,0,pre.length - 1);
// Let's start the non recursive version
// Traverse each element in the array traversed by the preceding sequence , Take each element as the root , Get their left and right subtrees ;
if (pre == null || pre.length == 0) {
return null;
}
int mid_index = 0;
TreeNode root = new TreeNode(pre[0]);
Deque<TreeNode> stack = new LinkedList<>();
stack.push(root);
// stay vin in [ The left subtree ] [ Right subtree ] root
// Sequence traversal results before sequence traversal !
// root [ The left subtree ][ Right subtree ]
for(int i = 1; i < pre.length; i++){
TreeNode cur = stack.peek();
int preorder_val = pre[i];
if(cur.val != vin[mid_index]){
// Until traversal vin[mid_index] Left subtree of root
cur.left = new TreeNode(preorder_val);
stack.push(cur.left);
}else {
while(!stack.isEmpty() && stack.peek().val == vin[mid_index]){
cur = stack.pop();
mid_index++;
}
cur.right = new TreeNode(preorder_val);
stack.push(cur.right);
}
}
return root;
}
// With pre[l_l] Traversal interval of subtree as root [l_l,l_r]
// With pre[l_l] The interval of the subtree as the root in the middle order traversal [r_l,r_r];
// Through constant partition , Solve the problem Finally, the solution of the small problem is merged Get the solution of the big problem
private TreeNode helper(int[] pre,int[] vin,int l_l,int l_r,int r_l,int r_r){
if(l_l > l_r) return null;
TreeNode root = new TreeNode(pre[l_l]);
// Find the position of the root in the middle order traversal , Then again subtract r_l , The result is the width of the left subtree rooted at the current node
int index_in = index(vin,root.val);
int wid = index_in - r_l; // Find the interval length of the left subtree
// Right subtree Section The interval of left subtree in preorder traversal & The middle order traverses the corresponding interval of the left subtree
root.left = helper(pre,vin,l_l + 1,l_l + wid,r_l,index_in - 1);
// The interval of right subtree subtree in preorder traversal & The middle order traverses the corresponding interval of the right subtree
root.right = helper(pre,vin,l_l + wid + 1,l_r,index_in+1,r_r);
return root;
}
// Traverse the position in the result set in the middle order
private int index(int[] vin,int val){
int ans = -1;
for(int i = 0; i < vin.length; i++){
if(vin[i] == val){
ans = i;
break;
}
}
return ans;
}
}
边栏推荐
- Shengxin literature learning (Part1) -- precision: a approach to transfer predictors of drug response from pre-clinical ...
- [rust notes] 04 expression
- SQL 注入漏洞(十四)xff 注入攻击
- [M32] simple interpretation of MCU code, RO data, RW data and Zi data
- 代理模式与装饰模式到底哪家强
- Geoswath plus technology and data acquisition and processing
- The tidb community offline exchange meeting was seen by the partners from Tianjin and Shijiazhuang~
- Information system project management - scope management (focus)
- SQL injection vulnerability (XIII) Base64 injection
- 成功解决raise KeyError(f“None of [{key}] are in the [{axis_name}]“)KeyError: “None of [Index([‘age.in.y
猜你喜欢
随机推荐
Detailed explanation of eight locks
[openairinterface5g] rrcsetuprequest for RRC NR resolution
[rust daily] January 23, 2022 webapi benchmarking
C skill tree evaluation - customer first, making excellent products
Armadillo安装
仙人掌之歌——上线运营(4)
[rust notes] 04 expression
[PHP]TP6 CLI模式下创建tp6和多应用配置以及常见问题
SQL 注入漏洞(十)二次注入
【openairinterface5g】项目目录结构
Reprint the Alibaba open source project egg JS technical documents cause "copyright disputes". How to use the loose MIT license?
WPS文档目录更新产生的问题记录
MySQL Niuke brush questions
SQL injection vulnerability (XII) cookie injection
[M32] SCM xxx Simple interpretation of map file
关于solidity的delegatecall的坑
OpenGL - Draw Triangle
【5G NR】NG接口
Cactus Song - online operation (4)
Data security practice guide - data collection security management




![[5g NR] ng setup of ngap protocol](/img/aa/bbe4b345374d2cf2ab3bfde5e94d7c.png)

![[PHP] composer 安装](/img/37/7adaca01b95085b42a116bc6b08165.png)

