判断是否AVL平衡二叉书

#include<iostream>
#include<vector>
#include<stack>
#include<string>
#include<queue>
#include<algorithm>
#include<numeric>
using namespace std; class node{
public:
int val;
node* left;
node* right;
node():val(),left(NULL),right(NULL){}
}; node* createTree()
{
node* head = new node[];
for(int i = ;i<;i++)
{
head[i].val = i;
if(*i+ < )
head[i].left = head + *i + ;
if(*i+ < )
head[i].right = head + *i + ;
}
return head;
} int depth(node* root)
{
if(root == NULL)
return ;
else{
int lef = depth(root->left);
int rig = depth(root->right);
return ((lef>rig)?lef:rig) + ;
}
} bool isblanced(node* root)
{
if(NULL == root)
return true;
else
{
int lef = depth(root->left);
int rig = depth(root->right);
int dif = lef - rig; if(dif == || dif == - || dif == )
return isblanced(root->left)&&isblanced(root->right);
else
return false;
}
} int main()
{
node* t = createTree();
cout<<isblanced(t);
}
上一篇:C# 关键字--virtual(转)


下一篇:线上BUG:MySQL死锁分析实战