当前位置:网站首页>105. constructing binary trees from preorder and inorder traversal sequences
105. constructing binary trees from preorder and inorder traversal sequences
2022-06-13 06:40:00 【Mr Gao】
105. Construction of binary trees from traversal sequences of preorder and middle order
Given two arrays of integers preorder and inorder , among preorder Is the prior traversal of a binary tree , inorder Is the middle order traversal of the same tree , Please construct a binary tree and return its root node .
Example 1:
Input : preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output : [3,9,20,null,null,15,7]
Example 2:
Input : preorder = [-1], inorder = [-1]
Output : [-1]
The solution code is as follows :
int root_preorder(int *preorder,int left,int right){
return preorder[left];
}
int find_root(int* inorder,int left,int right,int val){
int i;
for(i=left;i<=right;i++){
if(inorder[i]==val){
return i;
}
}
return 0;
}
int find_pre(int* preorder,int preleft,int preright,int* inorder,int inleft,int inright){
int i;
int j;
for(i=preleft;i<=preright;i++){
for(j=inleft;j<=inright;j++){
if(preorder[i]==inorder[j]){
return i;
}
}
}
return 0;
}
void create_tree_dfs(int* preorder,int preleft,int preright,int* inorder,int inleft,int inright,struct TreeNode **r){
if(preleft<=preright){
int val=root_preorder(preorder,preleft,preright);
(*r)->val=val;
(*r)->left=(struct TreeNode* )malloc(sizeof(struct TreeNode));
(*r)->right=(struct TreeNode* )malloc(sizeof(struct TreeNode));
int in_root_index=find_root(inorder,inleft,inright,val);
int left_index=find_pre(preorder,preleft,preright,inorder,inleft,in_root_index-1);
// printf("left_index %d |",left_index);
create_tree_dfs(preorder,left_index,left_index+in_root_index-inleft-1,inorder,inleft,in_root_index-1,&((*r)->left));
int right_index=find_pre(preorder,preleft,preright,inorder,in_root_index+1,inright);
// printf("right_index %d |",right_index);
create_tree_dfs(preorder,right_index,right_index+inright-in_root_index-1,inorder,in_root_index+1,inright,&((*r)->right));
}
else{
*(r)=NULL;
}
}
struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize){
struct TreeNode* r=(struct TreeNode* )malloc(sizeof(struct TreeNode));
create_tree_dfs(preorder,0,preorderSize-1,inorder,0,inorderSize-1,&r);
return r;
}
边栏推荐
- Detailed explanation of the player startup process of ijkplayer code walkthrough 2
- Kotlin basic definition class, initialization and inheritance
- [JS] handwriting call(), apply(), bind()
- 如何使用望友DFM软件进行冷板分析
- Kotlin collaboration -- context and exception handling
- c语言对文件相关的处理和应用
- RN Metro packaging process and sentry code monitoring
- The processing and application of C language to documents
- Two uses of bottomsheetbehavior
- Ijkplayer code walk through H264 unpacker application details
猜你喜欢
‘ipconfig‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
Omron Ping replaces the large domestic product jy-v640 semiconductor wafer box reader
Solution: vscode open file will always overwrite the last opened label
Kotlin collaboration -- context and exception handling
Ffmpeg compressed video.
Relationship between fragment lifecycle and activity
【2022高考季】作为一个过来人想说的话
MFS details (VII) -- MFS client and web monitoring installation configuration
JetPack - - - Navigation
[virtual machine] VMware virtual machine occupies too much space. Solution
随机推荐
[JS] array flattening
电镀挂具RFID工序管理解决方案
Kotlin data flow - flow
《MATLAB 神经网络43个案例分析》:第11章 连续Hopfield神经网络的优化——旅行商问题优化计算
机器学习笔记 - 监督学习备忘清单
Thread pool learning
MFS详解(六)——MFS Chunk Server服务器安装与配置
MFS詳解(七)——MFS客戶端與web監控安裝配置
[case] a super easy-to-use tool -- a calculator for programmers
Analyzing server problems using jvisualvm
【新手上路常见问答】一步一步理解程序设计
Usegeneratedkeys=true configuration
Logcat -b events and eventlogtags print the location correspondence of the events log in the code
Kotlin collaboration process +flow download case
BlockingQueue source code
Array operations in JS
Vector control of Brushless DC motor (4): sensorless control based on sliding mode observer
JS method of extracting numbers from strings
DataGridView data export to excel (in case of small amount of data)
[system analysis and design] college student association management system