二叉树求高度

#include<bits/stdc++.h>
using namespace std;
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};
void CreatBinTree(BinTree &BT);
int GetHeight( BinTree BT );
int main()
{
    BinTree BT;
    CreatBinTree(BT);
    char a[]="ABDFECGHI//ABD##FE###CG#H##I##";
    printf("%d\n", GetHeight(BT));
    return 0;
}
void CreatBinTree(BinTree &BT)
{
    char c;
    cin>>c;
    if('#'==c)
        BT=NULL;
    else
    {
        BT=new TNode;
        BT->Data=c;
        CreatBinTree(BT->Left);
        CreatBinTree(BT->Right);
    }
}
int GetHeight( BinTree BT )
{
  if(BT==NULL)
  {
    return 0;
  }
  int a=GetHeight(BT->Left);
  int b=GetHeight(BT->Right);
  return (a>b?a:b)+1;
}

 

posted @ 2018-08-24 10:54  MCQ  阅读(174)  评论(0编辑  收藏  举报