1. 二叉平衡树的概念
参见另一篇博客
2 如何判断一棵二叉树是平衡二叉树?
思路:
先编写计算二叉树高度的函数,然后再判断每个节点的左右子树是否相差1。
代码如下:
//判断二叉树的高度
template<typename T>
- int DepthTree(BSTreeNode<T> *pbs)
- {
- if (pbs==NULL)
- return 0;
- else
- {
- int leftLength=DepthTree(pbs->left);
- int rigthLength=DepthTree(pbs->right);
- return 1+(leftLength>rigthLength ? leftLength:rigthLength);
- }
- }
//判断二叉树是否是平衡二叉树(方法一)
- template<typename T>
- bool isBalanceTree(BSTreeNode<T> *pbs)
- {
- if (pbs==NULL)
- {
- return true;
- }
- int depthLeft=DepthTree(pbs->left);
- int depthRight=DepthTree(pbs->right);
- if (abs(depthLeft-depthRight)>1)
- return false;
- else
- return isBalanceTree(pbs->left) && isBalanceTree(pbs->right);
- }
//判断二叉树是否是平衡二叉树(方法二)
- template<typename T>
- bool isBalanceTree(BSTreeNode<T> *pbs, int *pDepth)
- {
- if (pbs==NULL)
- {
- *pDepth = 0;
- return true;
- }
- int left, right ;
- if (isBalanceTree(pbs->left,&left) && isBalanceTree(pbs->right,&right))
- {
- int dift = left – right;
- if(dift <=1 && dift >= -1)
- {
- *pDepth = 1 + (left > right ? left : right );
- return true;
- }
- }
- return false;
- }
: 方法一中一个节点要被重复遍历多次,时间效率不高;方法二使用了后序遍历的方法,只要遍历到一个节点之前我们已经遍历了它的左右子树。(参见剑指offer)
测试代码:
- #include<iostream>
- #include<cmath>
- using namespace std;
- template<typename T>
- struct BSTreeNode
- {
- T data;
- BSTreeNode *left;
- BSTreeNode *right;
- };
- template<typename T>
- int DepthTree(BSTreeNode<T> *pbs)
- {
- if (pbs==NULL)
- return 0;
- else
- {
- int leftLength=DepthTree(pbs->left);
- int rigthLength=DepthTree(pbs->right);
- return 1+(leftLength>rigthLength ? leftLength:rigthLength);
- }
- }
- template<typename T>
- bool isBalanceTree(BSTreeNode<T> *pbs)
- {
- if (pbs==NULL)
- {
- return true;
- }
- int depthLeft=DepthTree(pbs->left);
- int depthRight=DepthTree(pbs->right);
- if (abs(depthLeft-depthRight)>1)
- return false;
- else
- return isBalanceTree(pbs->left) && isBalanceTree(pbs->right);
- }
- void CreateTree1(BSTreeNode<int> *pHead)
- {
- int m;
- int i;
- int rNum;
- cin>>m;
- BSTreeNode<int> *currentNode;
- currentNode=pHead;
- pHead->left=NULL;
- pHead->right=NULL;
- cin>>currentNode->data;
- for(i=1;i<m;i++)
- {
- rNum=rand()%2;
- if (rNum==0)
- {
- BSTreeNode<int> *leftNode;
- leftNode=new BSTreeNode<int>;
- cin>>leftNode->data;
- leftNode->left=NULL;
- leftNode->right=NULL;
- currentNode->left=leftNode;
- currentNode=leftNode;
- }
- else
- {
- BSTreeNode<int> *rightNode;
- rightNode=new BSTreeNode<int>;
- cin>>rightNode->data;
- rightNode->right=NULL;
- rightNode->left=NULL;
- currentNode->right=rightNode;
- currentNode=rightNode;
- }
- }
- }
- void CreateTree2(BSTreeNode<int> *pHead)
- {
- int m;
- cin>>m;
- cin>>pHead->data;
- pHead->left=NULL;
- pHead->right=NULL;
- int i;
- BSTreeNode<int> *pCurrent;
- pCurrent=pHead;
- for (i=1;i<m;i++)
- {
- BSTreeNode<int> *pTemp;
- pTemp=new BSTreeNode<int>;
- pTemp->left=NULL;
- pTemp->right=NULL;
- cin>>pTemp->data;
- pCurrent->left=pTemp;
- pCurrent=pCurrent->left;
- }
- pCurrent=pHead;
- while (pCurrent!=NULL)
- {
- cout<<pCurrent->data;
- pCurrent=pCurrent->left;
- }
- }
- int main()
- {
- BSTreeNode<int> *head=NULL;
- head=new BSTreeNode<int>;
- CreateTree1(head);
- //CreateTree2(head);
- if (isBalanceTree(head))
- {
- cout<<“It is a Balance Tree!”<<endl;
- }else
- {
- cout<<“The tree is not Balance!”<<endl;
- }
- return 0;
- }