#include#include typedef char ElemType; typedef struct BTNode{ ElemType data; struct BTNode *lchild; struct BTNode *rchild; }BTNode,*BiTree; void createBiTree(BTNode *&B) { ElemType ch; scanf("%c",&ch); if(ch== ' ') B=NULL; else { B = (BTNode*)malloc(sizeof(BTNode)); B->data = ch; createBiTree(B->lchild); createBiTree(B->rchild); } } int countNode(BiTree B) { if(!B) return 0; else return 1 + countNode(B->lchild) + countNode(B->rchild); } void printPreOrder(BiTree B) { if(B) { printf("%c ",B->data); printPreOrder(B->lchild); printPreOrder(B->rchild); } } void main() { BiTree B; createBiTree(B); printf("先序遍历:"); printPreOrder(B); printf("n"); printf("结点个数:%dn",countNode(B)); }
按先序遍历来输入二叉树结点,若孩子结点为空,则输入空格。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)