#include<stdio.h> #include<string.h> struct node { char data; struct node *lchild,*rchild; }; char pre[105],in[105]; struct node *create(int prel,int prer,int inl,int inr) { if(prel>prer) { return NULL; } struct node *root=(struct node *)malloc(sizeof(struct node)); root->data=pre[prel]; int k; for(k=inl;in[k]!=root->data;k++); int numleft=k-inl; root->lchild=create(prel+1,prel+numleft,inl,k-1); root->rchild=create(prel+numleft+1,prer,k+1,inr); return root; } void postorder(struct node *root) { if(root==NULL) { return ; } postorder(root->lchild); postorder(root->rchild); printf('%c',root->data); } int main() { int n; while(scanf("%d",&n)!=EOF) { scanf("%s%s",pre,in); struct node *root=create(0,strlen(pre)-1,0,strlen(in)-1); postorder(root); printf("\n"); } return 0; }

C语言实现二叉树创建与后序遍历

原文地址: https://www.cveoy.top/t/topic/nGTA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录