实现由先序、中序序列构造二叉树的算法,实现由后序、中序序列构造二叉树的算法
来源:互联网 发布:金属探测软件 编辑:程序博客网 时间:2024/06/06 16:49
一道上机题 思想是递归
如果不理解原理可以看这个博客 注释里的说明很详细:http://blog.csdn.net/yunzhongguwu005/article/details/9270085
在代码的注释部分有测试样例
先序和中序:
#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<string>#include<stack>#include<queue>#include<cmath>#include<stack>#include<list>#include<map>#include<set>#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i>=y;i--)#define mem(a,x) memset(a,x,sizeof(a))#define gi(x) scanf("%d",&x)#define gi2(x,y) scanf("%d%d",&x,&y)#define gll(x) scanf("%lld",&x)#define gll2(x,y) scanf("%lld%lld",&x,&y)#define gc(x) scanf("%c",&x)#define gc2(x,y) scanf("%c%c",&x,&y)using namespace std;const double eps=1e-8; typedef long long ll;const int MAXN=100005;const int mod=1000000007;const ll llinf = (ll)(1e18) + 500;const int inf=0x3f3f3f3f;struct Node{char c;Node *lson;Node *rson;};void build(Node* &t,string prestr,string instr){if(prestr.size()==0){t=NULL;return;}char rootch=prestr[0];int index=instr.find(rootch);//下标string lsoninstr=instr.substr(0,index);string rsoninstr=instr.substr(index+1);int lsonlen=lsoninstr.size();//int rsonlen=rsoninstr.size();string lsonprestr=prestr.substr(1,lsonlen);string rsonprestr=prestr.substr(lsonlen+1);t=new Node;if(t){t->c=rootch;build(t->lson, lsonprestr, lsoninstr);build(t->rson, rsonprestr, rsoninstr);}}void preorderTraverse(Node* & t) { if(t!=NULL) { cout<<t->c; preorderTraverse(t->lson); preorderTraverse(t->rson); } } void inorderTraverse(Node* & t) { if(t!=NULL) { inorderTraverse(t->lson);cout<<t->c; inorderTraverse(t->rson); } } void postorderTraverse(Node* & t) { if(t!=NULL) { postorderTraverse(t->lson); postorderTraverse(t->rson); cout<<t->c; } } //string pre = "1234567";//string in = "3241657";//后序 3 4 2 6 7 5 1 int main(){int i,j,k;string prestr,instr;//prestr="ABCDEFG";//instr="CBEDAFG";prestr="1234567";instr="3241657";Node *root;build(root,prestr,instr);preorderTraverse(root);cout<<endl;inorderTraverse(root);cout<<endl;postorderTraverse(root);cout<<endl;return 0;}
#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<string>#include<stack>#include<queue>#include<cmath>#include<stack>#include<list>#include<map>#include<set>#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i>=y;i--)#define mem(a,x) memset(a,x,sizeof(a))#define gi(x) scanf("%d",&x)#define gi2(x,y) scanf("%d%d",&x,&y)#define gll(x) scanf("%lld",&x)#define gll2(x,y) scanf("%lld%lld",&x,&y)#define gc(x) scanf("%c",&x)#define gc2(x,y) scanf("%c%c",&x,&y)using namespace std;const double eps=1e-8; typedef long long ll;const int MAXN=100005;const int mod=1000000007;const ll llinf = (ll)(1e18) + 500;const int inf=0x3f3f3f3f;struct Node{char c;Node *lson;Node *rson;};void build(Node* &t,string poststr,string instr){if(poststr.size()==0){t=NULL;return;}char rootch=poststr[poststr.size()-1];int index=instr.find(rootch);//下标string lsoninstr=instr.substr(0,index);string rsoninstr=instr.substr(index+1);int lsonlen=lsoninstr.size();int rsonlen=rsoninstr.size();string lsonpoststr=poststr.substr(0,lsonlen);string rsonpoststr=poststr.substr(lsonlen,poststr.size()-1-lsonlen);//string rsonpoststr=rsonpoststrtmp.substr(0,);t=new Node;if(t){t->c=rootch;build(t->lson, lsonpoststr, lsoninstr);build(t->rson, rsonpoststr, rsoninstr);}}void preorderTraverse(Node* & t) { if(t!=NULL) { cout<<t->c; preorderTraverse(t->lson); preorderTraverse(t->rson); } } void inorderTraverse(Node* & t) { if(t!=NULL) { inorderTraverse(t->lson);cout<<t->c; inorderTraverse(t->rson); } } void postorderTraverse(Node* & t) { if(t!=NULL) { postorderTraverse(t->lson); postorderTraverse(t->rson); cout<<t->c; } } //string pre = "1234567";//string in = "3241657";//后序 3 4 2 6 7 5 1 int main(){int i,j,k;string poststr,instr;//prestr="ABCDEFG";//instr="CBEDAFG";poststr="3426751";instr="3241657";Node *root;build(root,poststr,instr);preorderTraverse(root);cout<<endl;inorderTraverse(root);cout<<endl;postorderTraverse(root);cout<<endl;return 0;}
阅读全文
0 0
- 实现由先序、中序序列构造二叉树的算法,实现由后序、中序序列构造二叉树的算法
- 由先序序列/后序序列和中序序列构造二叉树
- 第十一周项目1--(2)二叉树构造算法的验证--由先序序列和中序序列构造二叉树
- 第11周项目1-(2)二叉树构造算法的验证、由先序序列和中序序列构造二叉树
- 由中序遍历序列和后序遍历序列构造二叉树(递归实现)
- 第十一周项目1--(2)二叉树构造算法的验证--由后序序列和中序序列构造二叉树
- 第11周项目1-(2)二叉树构造算法的验证、由后序序列和中序序列构造二叉树
- 先序序列和中序序列构造二叉树,中序序列和后序序列构造二叉树
- 第11周—项目1(2).1由先序序列和中序序列构造二叉树
- 第十一周项目1(2)由先序序列和中序序列构造二叉树
- 第十一周【项目一-(2-1)由先序序列和中序序列构造二叉树】
- 第十周项目1(2)-由先序序列和中序序列构造二叉树
- 第11周项目1-(2)。1由先序序列和中序列构造二叉树
- 由先序遍历和中序遍历构造二叉树的二叉链表代码
- 第十一周项目2 二叉树构造算法--先序序列和中序序列构造算法
- 第十一周项目2 二叉树构造算法--先序序列和中序序列构造算法
- 由二叉树的后序序列和中序序列构成二叉树
- 如何由二叉树的先序和中序序列画出二叉树
- 脆弱 VS 可靠性
- Android使用AIDL实现进程间通信
- SaaS服务商如何通过数加平台统计业务流量
- 欢迎使用CSDN-markdown编辑器11111111
- TCP/IP详解卷1:协议(十二)【TCP的坚持定时器,保活定时器】
- 实现由先序、中序序列构造二叉树的算法,实现由后序、中序序列构造二叉树的算法
- Building your Deep Neural Network
- [saiku] 通过管理台配置用户、schema和数据源
- centos 下安装nsq,配置开机启动服务
- HAL层打开Camera底层驱动
- 图
- Android OOM原因总结
- 利用HashMap辅助调整对象结构
- 微信支付:curl出错,错误码:60