当前位置:网站首页>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;
}
}
边栏推荐
- Clickhouse compares data from two machines
- [PHP] composer 安装
- MySQL ifnull processing n/a
- [rust notes] 01 basic types
- [rust notes] 04 expression
- Shengxin literature learning (Part1) -- precision: a approach to transfer predictors of drug response from pre-clinical ...
- Why did I choose rust
- -Bash: telnet: command not found solution
- Pytest数据参数化&数据驱动
- Flink core features and principles
猜你喜欢
![[5g NR] mobile phone ID number IMEI and imeisv](/img/f0/2613fc9f59f7d0d4335b13f0273fc2.png)
[5g NR] mobile phone ID number IMEI and imeisv

SQL 注入漏洞(十一)宽字节注入

OpenGL - Draw Triangle

What exactly is the open source office of a large factory like?

Using Monte Carlo method to calculate pi

Performance comparison and analysis

Leetcode the shortest path of three (eight) charts per week
![[5g NR] RRC connection reconstruction analysis](/img/7a/6f9942b1874604664924e22e04d516.png)
[5g NR] RRC connection reconstruction analysis

5G-GUTI详解

代理模式与装饰模式到底哪家强
随机推荐
SQL 注入漏洞(十)二次注入
《数据安全实践指南》- 数据采集安全管理
Event preview edgex developer summit @ Nanjing station is coming!
IO intensive and CPU intensive
The song of cactus - marching into to C live broadcast (2)
OpenGL - Draw Triangle
C skill tree evaluation - customer first, making excellent products
Cactus Song - March to C live broadcast (3)
SQL injection vulnerability (XIV) XFF injection attack
SQL 注入漏洞(十三)base64注入
[tp6] using the workman websocket
Data security practice guide - data collection security practice - data classification and classification
CGIC file upload - rookie notes
Don't throw away the electric kettle. It's easy to fix!
Introduction to PMOD interface of kv260
Py之scorecardpy:scorecardpy的简介、安装、使用方法之详细攻略
博客添加邮箱私信 快捷
SQL injection vulnerability (x) secondary injection
[5g NR] ng interface
[openairinterface5g] RRC NR resolution (I)