二叉树的建立及检验
作者:互联网
//测试数据
// 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);
}
标签:Right,CreateTree,建立,BiTNode,检验,num,二叉树,data,Left 来源: https://www.cnblogs.com/summer-4/p/15490769.html