#include<iostream>#include<string.h>using namespace std;typedef struct BiTNode{char data;struct BiTNode *lchild,*rchild;}BiTNode, *BiTree;int Create(BiTree &T){char data;cin>>data;if(data==','){ T=NULL;}else{T=new BiTNode;T->data=data;Create(T->lchild);Create(T->rchild);}return 0;}void visit(BiTree T){if(T->data!=',')cout<<T->data;}void InOrder(BiTree T){if(T!=NULL){InOrder(T->lchild);visit(T);InOrder(T->rchild);}}void PostOrder(BiTree T){if(T!=NULL){PostOrder(T->lchild);PostOrder(T->rchild);visit(T);}}int count_tree(BiTree T,int &n){if(T){if(T->lchild==NULL&&T->rchild==NULL)n++;count_tree(T->lchild,n);count_tree(T->rchild,n);}return n;}int high_tree(BiTree T){int h,left,right;if(!T){ return 0;}left=high_tree(T->lchild);right=high_tree(T->rchild);h=(left>right?left:right)+1;return h;}int main(){int n,a,b=0;BiTree T;Create(T);InOrder(T);cout<<"\n";PostOrder(T);cout<<"\n";a=count_tree(T,b);cout<<a<<endl;n=high_tree(T);cout<<n<<endl;return 0;}
Build and traverse a binary tree in the data structure experiment of sdutoj 2136