二叉树的遍历
来源:互联网 发布:u深度安装linux 编辑:程序博客网 时间:2024/06/05 05:56
前序,中序-->后序
#include<stdio.h>#include<string.h>int a[1010],b[1010];int flag;void build(int x1,int y1,int x2,int y2){ if(x1>y1) return ; int x=a[x1]; int p=x2; while(b[p]!=x) p++; int cnt=p-x2; //左子树的个数 build(x1+1,x1+cnt,x2,p-1); build(x1+cnt+1,y1,p+1,y2); if(flag==0) { printf("%d",x); flag=1; } else printf(" %d",x); return ;}int main(){ int t; while(~scanf("%d",&t)) { flag=0; for(int i=0; i<t; i++) scanf("%d",&a[i]); //前序 for(int j=0; j<t; j++) scanf("%d",&b[j]); //中序 build(0,t-1,0,t-1); printf("\n"); } return 0;}
中序,后序-->前序
#include<algorithm>#include<string.h>#include<queue>using namespace std;#define MAX 1010int a[MAX];//中序int b[MAX];//后序void build(int x1,int y1,int x2,int y2){ if(x2>y2) return; int x=b[y2]; printf("%d ",x);//输出前序 int p=x1; while(a[p]!=x) p++; int cnt=p-x1; build(x1,p-1,x2,x2+cnt-1); build(p+1,y1,x2+cnt,y2-1); return ;}int main(){ int t; while(~scanf("%d",&t)) { for(int i=0; i<t; i++) scanf("%d",&a[i]); //中序 for(int j=0; j<t; j++) scanf("%d",&b[j]); //后序 build(0,t-1,0,t-1); printf("\n"); } return 0;}
中序,前序-->层序
#include<stdio.h>#include<algorithm>#include<string.h>#include<queue>using namespace std;#define MAX 100int a[MAX],b[MAX];struct Node{ int lch; int rch;} s[MAX];int build(int x1,int y1,int x2,int y2)//中序,前序{ if(x2>y2) return 0; int x=b[x2]; int p=x1; while(a[p]!=x) p++; int cnt=p-x1; s[x].lch=build(x1,p-1,x2+1,x2+cnt); s[x].rch=build(p+1,y1,x2+cnt+1,y2); return x;}void print(int x){ queue<int>Q; Q.push(x); int flag=0; while(!Q.empty()) { int t=Q.front(); Q.pop(); printf("%d ",t);//输出层序遍历 if(s[t].lch!=0) Q.push(s[t].lch); if(s[t].rch!=0) Q.push(s[t].rch); } printf("\n");}int main(){ int n; scanf("%d",&n); for(int i=0; i<n; i++) scanf("%d",&a[i]);//中 for(int j=0; j<n; j++) scanf("%d",&b[j]);//前 build(0,n-1,0,n-1); print(b[0]);//根 return 0;}
中序,后序-->层序
#include<stdio.h>#include<algorithm>#include<string.h>#include<queue>using namespace std;#define MAX 100int a[MAX],b[MAX];struct Node{ int lch; int rch;} s[MAX];int build(int x1,int y1,int x2,int y2)//中序,后序{ if(x2>y2) return 0; int x=b[y2]; int p=x1; while(a[p]!=x) p++; int cnt=p-x1; s[x].lch=build(x1,p-1,x2,x2+cnt-1); s[x].rch=build(p+1,y1,x2+cnt,y2-1); return x;}void print(int x){ queue<int>Q; Q.push(x); int flag=0; while(!Q.empty()) { int t=Q.front(); Q.pop(); printf("%d ",t); if(s[t].lch!=0) Q.push(s[t].lch); if(s[t].rch!=0) Q.push(s[t].rch); } printf("\n");}int main(){ int n; scanf("%d",&n); for(int i=0; i<n; i++) scanf("%d",&a[i]);//中 for(int j=0; j<n; j++) scanf("%d",&b[j]);//后 build(0,n-1,0,n-1); print(b[n-1]);//根 return 0;}
0 0
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- RXAndroid-exception:rx.exceptions.MissingBackpressureException
- TLD目标跟踪算法学习(二)
- 实验五:实验报告
- shutdown停止tomcat
- 【RocketMQ】搭建RocketMQ
- 二叉树的遍历
- js把long型日期还原成yyyy-MM-dd 00:00:00或yyyy-MM-dd
- 封装一个Button的角标类(Image等同理可以实现)
- 项目开发中遇到的一些问题--阻止浏览器错误保存cookie
- 顺序表的相关算法
- NYOJ 522 Interval (树状数组)
- ld: library not found for -lXXXXX 编译问题的解决方法
- shell 实现读取文件某行的内容
- iOS