#include<stdio.h>typedefintTelemType;
typedefstructBinaryTreeNode{
TelemTypedata;
structBinaryTreeNode*Left;
structBinaryTreeNode*Right;
}Node;
Node*createBinaryTree()
{
Node*p;
TelemTypech;
scanf("%d",&ch);
if(ch==0) {
p=NULL;
}
else {
p= (Node*)malloc(sizeof(Node));
p->data=ch;
p->Left=createBinaryTree(); p->Right=createBinaryTree(); }
returnp;
}
voidpreOrderTraverse(Node*root)
{
if( root )
{
printf("%d ",root->data);
preOrderTraverse(root->Left);
preOrderTraverse(root->Right);
}
}
voidinOrderTraverse(Node*root)
{
if( root )
{
inOrderTraverse(root->Left);
printf("%d ",root->data);
inOrderTraverse(root->Right);
}
}
voidlastOrderTraverse(Node*root)
{
if( root )
{
lastOrderTraverse(root->Left);
lastOrderTraverse(root->Right);
printf("%d ",root->data);
}
}
intmain()
{
Node*root=NULL;
root=createBinaryTree();
printf("二叉树建立成功");
printf("\n");
printf("前序遍历结果:\n");
preOrderTraverse(root);
printf("中序遍历结果:\n");
inOrderTraverse(root);
printf("后序遍历结果:\n");
lastOrderTraverse(root);
return0;
}