先序序列创建二叉树

Description

给定先序序列,按照该序列创建对应的二叉树,并输出其中序和后序序列。

Input

一行,二叉树按先序遍历序列,空指针用字符^占位

Output

两行,分别对应该二叉树的中序和后序序列

Sample Input
ABC^DEG^F^^

Sample Output
CBEGDFA
CGEFDBA

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Tree{
	struct Tree *lChild,*rChild;
	char data;
}*BiTree,BiTNode;
BiTree CreatBiTree()
{
	BiTree T;
	char ch;
	ch = getchar();
	if(ch != '^')
	{
		T = (BiTree)malloc(sizeof(BiTNode));
		T->data = ch;
		T->lChild = CreatBiTree();
		T->rChild = CreatBiTree();
	}
	else
	{
		T = NULL;
	}
	return T;
}
void InOderTraverse(BiTree T)
{
    if(T==NULL)
        return ;
    InOderTraverse(T->lChild);
    printf("%c",T->data);
    InOderTraverse(T->rChild);
} 
void PostOderTraverse(BiTree T)
{
    if(T==NULL)
        return;
    PostOderTraverse(T->lChild);
    PostOderTraverse(T->rChild);
    printf("%c",T->data);
} 
int main(void)
{
	BiTree T;
	T = CreatBiTree();
	InOderTraverse(T);
	printf("\n");
	PostOderTraverse(T);
	return 0;
}```

原创文章 382 获赞 114 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_45949073/article/details/105862771