求二叉树高度(20 分)

来源:互联网 发布:苹果一体机软件下载 编辑:程序博客网 时间:2024/05/19 00:42

求给定二叉树的高度

#include <stdio.h>#include <stdlib.h>#include<iostream>using namespace std;typedef char ElementType;typedef struct TNode *Position;typedef Position BinTree;struct TNode{    ElementType Data;    BinTree Left;    BinTree Right;};BinTree CreatBinTree(){    BinTree BT;    ElementType ch;    cin>>ch;    if(ch=='#')//结点为空    {        BT=NULL;    }    else    {        BT=new TNode;//新节点        BT->Data=ch;        BT->Left=CreatBinTree();//递归建立左子树        BT->Right=CreatBinTree();    }    return BT;}int GetHeight( BinTree BT ){    if(BT==NULL)//数位空        return 0;    else    {        int m=GetHeight(BT->Left);//左子树高度        int n=GetHeight(BT->Right);//右子树高度        if(m>n)            return m+1;//子树高度加一        else            return n+1;    }}int main(){    BinTree BT = CreatBinTree();   printf("%d\n", GetHeight(BT));    return 0;}
原创粉丝点击