二叉樹的先序建樹後序輸出
阿新 • • 發佈:2018-12-21
程式碼~:
#include <stdio.h> #include <malloc.h> typedef struct Node { char root; struct Node *lchild,*rchild; } BiTNode,*BiTree; BiTree CreateBiTree()//先序建樹 { BiTree T; char ch; if((ch = getchar() )== '#') return 0; else { T = (BiTNode*)malloc(sizeof(BiTNode)); T->root = ch; T->lchild = CreateBiTree(); T->rchild = CreateBiTree(); return T; } } void postorder(BiTree T)//後序輸出 { if(T) { postorder(T->lchild); postorder(T->rchild); printf("%c",T->root); } } int main() { BiTree T = NULL; T = CreateBiTree(); postorder(T); printf("\n"); return 0; }