首页 > 其他 > 详细

判断是否AVL平衡二叉书

时间:2014-05-26 06:48:18      阅读:280      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
#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);
}
bubuko.com,布布扣

 

判断是否AVL平衡二叉书,布布扣,bubuko.com

判断是否AVL平衡二叉书

原文:http://www.cnblogs.com/berkeleysong/p/3747921.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!