求節點數

#include<iostream>
using namespace std;

typedef struct BiNode{
    char data;
    struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;

void CreateBiTree(BiTree &T){
    char ch;
    cin>>ch;
    if(ch=='#') T=NULL;
    else{
        T=new BiTNode;
        T->data=ch;
        CreateBiTree(T->lchild);
        CreateBiTree(T->rchild);
    }
}

int NodeCount(BiTree T){
    if(T==NULL) return 0;
    else return NodeCount(T->lchild)+NodeCount(T->rchild)+1;
}

void main(){
    BiTree tree;
    cout<<"請輸入創建二叉鏈表的序列:\n";
    CreateBiTree(tree);
    cout<<"結點個數爲:"<<NodeCount(tree)<<endl;
}
相關文章
相關標籤/搜索