cs
typedef struct Node
{
int data;
int bf;//平衡因子
struct Node* lchild;
struct Node* rchild;
}Node;
//创建节点
Node* Create(int data, int bf)
{
Node* newnode = (Node*)malloc(sizeof(Node));
newnode->data = data;
newnode->bf = bf;
newnode->lchild = newnode->rchild = NULL;
return newnode;
}
int Height(Node* root)
{
int level = 0;
Node* T = root;
while (T)
{
level++;//根节点存在++1;
if (T->bf < 0)
{
//<0那就是右边 > 左边
T = T->rchild;//找更深的分支下去
}
else
{
T = T->lchild;
}
}
return level;
}
int main()
{
Node* root = Create(28, 0);
root->lchild = Create(25, 1);
root->rchild = Create(31, 0);
root->lchild->lchild = Create(16, 0);
root->rchild->lchild = Create(30, 0);
root->rchild->rchild = Create(47, 0);
int high = Height(root);
printf("这棵平衡二叉树的高度为%d", high);
return 0;
}