先序和中序构造二叉树

#include <iostream>
using namespace std;

void getlast(int n,int *a,int *b);

int main() {
int n;
cin >> n;
int prescan[40], midscan[40];
int i;
for (i = 0; i < n; i++) {
cin >> prescan[i];
}
for (i = 0; i < n; i++) {
cin >> midscan[i];
}
getlast(n,prescan,midscan);
cout<<endl;
system("pause");
return 0;
}


void getlast(int n,int *a,int *b)
{
if(n<=0){
return ;
}
int root=a[0];
int i;
for(i=0;i<n;i++){
if(b[i]==root){
break;
}
}

getlast(i,a+1,b);
getlast(n-i-1,a+1+i,b+1+i);
cout<<root<<" ";
}

posted @   秋渡晚枫  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示