//测试数据
// 1 2 3 -1 -1 -1 4 -1 -1
include//有个C++的头文件显示不出来
include<malloc.h>
using namespace std;
typedef struct BiTNode
{
int data;
struct BiTNode *Left, *Right;
}BiTNode, *BiTree;
int CreateTree(BiTree &T)
{
int num;
scanf_s("%d", &num);
if (num == -1)
{
T = NULL;
return 0;
}
else
{
//按先序遍历输入数据
if ((T = (BiTNode)malloc(sizeof(BiTNode))))
{
T->data = num;
CreateTree(T->Left);
CreateTree(T->Right);
}
//按中序遍历输入数据
//if (!(T = (BiTNode)malloc(sizeof(BiTNode))))
//{
// CreateTree(T->Left);
// T->data = num;
// CreateTree(T->Right);
//}
//按后序遍历输入数据
//if (!(T = (BiTNode*)malloc(sizeof(BiTNode))))
//{
// CreateTree(T->Left);
// CreateTree(T->Right);
// T->data = num;
//}
}
}
//先序遍历二叉树
void ProTraverse(BiTree T)
{
if (T != NULL)
{
printf("%d", T->data);
ProTraverse(T->Left);
ProTraverse(T->Right);
}
}
int main()
{
BiTree p;
CreateTree(p);
ProTraverse(p);
}