参考资料:http://blog.csdn.net/u010442302/article/details/52713585
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
using namespace std;
typedef struct node
{
char data;
struct node *L_Child,*R_Child;
}Tree;
void initTree(Tree *&T)
{
char str;
cin>>str;
if(str!='#')
{
T=(Tree *)malloc(sizeof(Tree));
T->data=str;
initTree(T->L_Child);
initTree(T->R_Child);
}
else T=NULL;
}
int Deepth_of_Tree(Tree *T)
{
if(T!=NULL)
{
int sum1=Deepth_of_Tree(T->L_Child);
int sum2=Deepth_of_Tree(T->R_Child);
if(fabs(sum1-sum2)>1)
{
cout<<"no!";
exit(0);
}
return (sum1++)>(sum2++)?sum1:sum2;
}
return 0;
}
int main()
{
Tree *T;
initTree(T);
Deepth_of_Tree(T);
cout<<"yes!";
return 0;
}
本文介绍了一个使用C++实现的二叉树结构,并通过递归算法来初始化二叉树以及计算其深度,同时判断该二叉树是否为平衡二叉树。通过输入字符节点来构建二叉树,并利用递归方式计算左右子树的深度,最终确定整棵树的平衡状态。

1229

被折叠的 条评论
为什么被折叠?



