1 // #include <stdio.h> 2 // #include <stdlib.h> 3 4 // typedef char ElementType; 5 // typedef struct TNode *Position; 6 // typedef Position BinTree; 7 // struct TNode{ 8 // ElementType Data; 9 // BinTree Left; 10 // BinTree Right; 11 // }; 12 13 // BinTree CreatBinTree(); /* 实现细节忽略 */ 14 // void PreorderPrintLeaves( BinTree BT ); 15 16 // int main() 17 // { 18 // BinTree BT = CreatBinTree(); 19 // printf("Leaf nodes are:"); 20 // PreorderPrintLeaves(BT); 21 // printf("\n"); 22 23 // return 0; 24 // } 25 26 void PreorderPrintLeaves( BinTree BT ) 27 { 28 if (!BT) 29 { 30 31 } 32 else if (!BT->Left && !BT->Right) 33 { 34 printf(" %c", BT->Data); 35 } 36 else if (!BT->Right && BT->Left) 37 { 38 PreorderPrintLeaves(BT->Left); 39 } 40 else if (!BT->Left && BT->Right) 41 { 42 PreorderPrintLeaves(BT->Right); 43 } 44 else 45 { 46 PreorderPrintLeaves(BT->Left); 47 PreorderPrintLeaves(BT->Right); 48 49 } 50 }
PTA数据结构与算法题目集(中文)6-11 先序输出叶结点 (15分)
原文:https://www.cnblogs.com/ccvv/p/12445915.html