首页 > 代码库 > 判断是否AVL平衡二叉书

判断是否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(0),left(NULL),right(NULL){}
};

node* createTree()
{
    node* head = new node[14];
    for(int i = 0;i<10;i++)
    {
        head[i].val = i;
        if(2*i+1 < 10)
        head[i].left = head + 2*i + 1;
        if(2*i+2 < 10)
        head[i].right = head + 2*i + 2;
    }
    return head;
}


int depth(node* root)
{
  if(root == NULL)
      return 0;
  else{
      int lef = depth(root->left);
      int rig = depth(root->right);
      return ((lef>rig)?lef:rig) + 1 ;
  }
}

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 == 0 || dif == -1 || dif == 1)
         return isblanced(root->left)&&isblanced(root->right);
     else
     return false;
 }
}

int main()
{
    node* t = createTree();
    cout<<isblanced(t);
}