标签:des io ar os sp amp ef size as
#include <iostream> #include <cstdio> #include<algorithm> #include<cstdlib> using namespace std; struct Node { char data; Node *lchild; Node *rchild; }; int nodes(Node *T) { if(T==NULL) return 0; else if(T->lchild==NULL&&T->rchild==NULL) return 1; else return nodes(T->lchild)+nodes(T->rchild)+1; } void CountLeaf(Node *T,int &num) { if(T!=NULL) { if(T->lchild==NULL&&T->rchild==NULL) num++; CountLeaf(T->lchild,num); CountLeaf(T->rchild,num); } } void High(Node *T, int &h) { if (T == NULL) h = 0; else { int left_h; High(T->lchild, left_h); int right_h; High(T->rchild, right_h); h = 1 + max(left_h, right_h); } } Node *CreateBiTree(Node *&T) { char ch; cin>>ch; if (ch == '#') T = NULL; else { if (!(T = (Node *)malloc(sizeof(Node)))) return 0; T->data = ch; CreateBiTree(T->lchild); CreateBiTree(T->rchild); return T; } } void Free(Node *&T) { if (T == NULL) return; Free(T->lchild); // T->lchild = NULL; Free(T->rchild); // T->rchild = NULL; free(T); T = NULL; } int main( ) { Node *T = NULL; CreateBiTree(T); int num=0; int height; High(T, height); cout<<height<<endl; CountLeaf(T,num); cout<<num<<endl; Free(T); return 0; } /* cin.txt: A B C # # D E # G # # F # # # */
标签:des io ar os sp amp ef size as
原文地址:http://blog.csdn.net/u013514722/article/details/40892085