-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path7-5.cpp
More file actions
executable file
·45 lines (45 loc) · 918 Bytes
/
7-5.cpp
File metadata and controls
executable file
·45 lines (45 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include <string.h>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BSTree;
void CreateBiTree(BSTree &T,char a[],int &i)
{//先序建立二叉树
if(a[i]=='#')
T=NULL;
else
{
T=new BiTNode;
T->data=a[i];
CreateBiTree(T->lchild,a,++i);
CreateBiTree(T->rchild,a,++i);
}
}
int Height(BSTree T)
{//求平衡二叉树T的高度
int level=0; //记录树的高度
BSTree p=T;
while(p)
{
level++; //树的高度加1
if(p->data<0)p=p->rchild; //b=-1沿右分支向下
else p=p->lchild; //b>=0沿左分支向下
}
return level; //返回平衡二叉树的高度
}
int main()
{
char a[100]; //如果平衡因子b=-1,会被字符数组割裂成'-'和'1',故本题输入样例不包含b=-1
while(cin>>a)
{
if(strcmp(a,"#")==0) break;
int i=-1;
BSTree T;
CreateBiTree(T,a,++i);
cout<<Height(T)<<endl;
}
return 0;
}