文章目录
#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
void CreatBinTree(BinTree &BT);
void PreorderPrintLeaves(BinTree BT);
int main()
{
BinTree BT;
CreatBinTree(BT);
printf("Leaf nodes are:");
PreorderPrintLeaves(BT);
printf("\n");
system("PAUSE");
return 0;
}
void CreatBinTree(BinTree &BT){
ElementType p;
scanf("%c", &p);
getchar();
if (p == '#'){
BT = NULL;
}
else{
BT = (BinTree)malloc(sizeof(struct TNode));
BT->Data = p;
CreatBinTree(BT->Left);
CreatBinTree(BT->Right);
}
return;
}
void PreorderPrintLeaves(BinTree BT){
if (BT == NULL)return;
if (BT->Left == NULL&&BT->Right == NULL)
printf(" %c", BT->Data);
PreorderPrintLeaves(BT->Left);
PreorderPrintLeaves(BT->Right);
return;
}