pre_mid_bitree.cpp

来自「由二叉树的前序遍历结果与中序遍历结果来确定一棵二叉树。」· C++ 代码 · 共 50 行

CPP
50
字号
#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct node
{
	char data;
	struct node *lchild,*rchild;
};
typedef struct node NODE;
#define MAXN 50
char p[MAXN],m[MAXN];

NODE *create(char p[],char m[],int n)
{
	NODE *root;
	int posi;
	if(n<=0)return NULL;
	root=(NODE *)malloc(sizeof(NODE));
	root->data=p[0];
	for(posi=0;posi<n&&m[posi]!=root->data;posi++);
	root->lchild=create(p+1,m,posi);
	root->rchild=create(p+1+posi,m+posi+1,n-posi-1);
	return root;
}
void midorder(NODE *t)
{
	if(t!=NULL)
	{
		midorder(t->lchild);
		printf("%c",t->data);
		midorder(t->rchild);
	}
}
void main()
{
	NODE *root;
	int n,i;
	printf("Please input the array of preordered:");
	scanf("%s",p);
	printf("Please input the array of midordered:");
	scanf("%s",m);
	for(i=0;p[i]!='\0';i++);
	n=i;
	root=create(p,m,n);
	printf("The midorder array is:");
	midorder(root);
	printf("\n");
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?