当前位置:网站首页>Sword *offer04 rebuild binary tree
Sword *offer04 rebuild binary tree
2022-07-24 00:55:00 【wzf6667】
Title Description
Enter the results of preorder traversal and inorder traversal of a binary tree , Please rebuild the binary tree . It is assumed that the results of the input preorder traversal and the middle order traversal do not contain repeated numbers . For example, input preamble traversal sequence {1,2,4,7,3,5,6,8} And middle order ergodic sequence {4,7,2,1,5,3,8,6}, Then rebuild the binary tree and return .
Answer key
Preorder traversal is to output first root The node is on the left node and then on the right node , Middle order traversal is first left then root Last right node
Traverse... According to the precedence , We can know the of this tree root node , namely root = pre[0], Then go to the middle order traversal to find root node , Middle order traversal root The left of the node is the left subtree , The one on the right is the right subtree .
- Preface 1 2 4 7 3 5 6 8 ->root node 1
Middle preface 4,7,2,1,5,3,8,6
The left subtree Right subtree
So we can get
1 2 4 7 3 5 6 8->2 4 7 For the left subtree ,5 6 8 For the right subtree - recursive Regard the left and right subtrees as one tree ,root.left = reConstructBinaryTree(pr,ino);
root.right = reConstructBinaryTree(pr,ino);
Of course , If there is no number in the first sequence , That's it root The left side of the node / The right node is empty . - return root node .
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
TreeNode root = new TreeNode(pre[0]);
int length = pre.length;
if(length==1){
root.left = null;
root.right = null;
return root;
}
int location=0;
for(int i = 0;i<in.length;i++){
if(root.val == in[i]){
location = i;
}
}
if(location>0){
int[] pr = new int[location];
int[] ino = new int[location];
for(int j =0;j<location;j++){
pr[j]=pre[j+1];
ino[j]=in[j];
}
root.left=reConstructBinaryTree(pr,ino);
}else{
root.left =null;}
if(length-location-1>0){
int[] pr=new int[length-location-1];
int[] ino=new int[length-location-1];
for(int j=location+1;j<length;j++){
ino[j-location-1]=in[j];
pr[j-location-1]=pre[j];
}
root.right=reConstructBinaryTree(pr,ino);
}else{
root.right=null;
}
return root;
}
}
边栏推荐
- Educational Codeforces Round 132 (Rated for Div. 2) D. Rorororobot
- How can dbcontext support the migration of different databases in efcore advanced SaaS system
- Problem note - unable to open include file: "direct.h": no such file or directory
- Tutorial on principles and applications of database system (051) -- MySQL query (XIII): using queries in DML statements
- mysql 分支语句case报错
- AVX instruction set accelerated matrix multiplication
- What impact does the European "gas shortage" have on China?
- Solve the error: uncaught (in promise) navigationduplicated: avoided redundant navigation to current location:“
- Tutorial on the principle and application of database system (044) -- MySQL query (VI): using the limit option to realize paging query
- MySQL common commands
猜你喜欢

What impact does the European "gas shortage" have on China?

【LeetCode第 83 场双周赛】

Bean validation usage article ----05

postman测试接口在URL配置正确的情况下出现404或者500错误

Redis | very important Middleware

Treatment of particle boundary collision

Fpga:ov7725 camera displays images in rgb565 format through vga/hdmi
CA digital certificate

EFCore高级Saas系统下单DbContext如何支持不同数据库的迁移

多源文件方式去访问全局变量的方式(extern用法)
随机推荐
Summary of the fourth week of summer vacation
Summary of polynomial commitment schemes
Tutorial on principles and applications of database system (042) -- MySQL query (4): using wildcards to construct query conditions
Xilinx FPGA one way clock input two PLLs
采坑websocket总结
Tutorial on principles and applications of database system (039) -- MySQL query (I): syntax analysis of select command
Coloring old photos - deoldify get started quickly
Client does not support authentication protocol requested by server; consider upgrading MySQL client
如何在自动化测试中使用MitmProxy获取数据返回?
Tutorial on principles and applications of database system (043) -- MySQL query (V): Sorting Query Results
網絡系統實驗:ping不通的問題解决
C language: deep analysis of const keyword
采坑websocket總結
Bean Validation自定义容器验证篇----06
《天幕红尘》笔记与思考(五)强势文化与弱势文化
Printf function - conversion description
postman测试接口在URL配置正确的情况下出现404或者500错误
The salary of a tester who has worked for 3 years after job hopping is twice that of the original. The secret is
Classic example of C language - loan balance
Installation and use of appscan