1.输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中的任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。
分析: 首先要的获得二叉树的深度信息,很明显,当二叉树只有一个结点时,深度为1,当二叉树只有左子树时,深度为左子树的深度+1,当只有右子树时,高度为右子树的高度+1,当左右子树都有时,为左右子树中深度的较大值+1.可以采用递归的方式实现。这样可以获得二叉树的深度信息。 然后,再判断二叉树是否满足平衡二叉树的条件。前面已经计算得到树的深度的信息,基于此,可以对树的每个结点的左右子树的深度信息是否满足条件,但是这种方法存在着大量的重复的计算。如果使用后序遍历的方式遍历二叉树的每个结点,在每遍历到一个结点之前我们就已经遍历了它的左右子树。这样在遍历每个结点的时候记录它的深度,就可以一边遍历一边判断是否满足条件。
源码:
/**
* 功能说明:Description
* 作者:K0713
* 日期:2016-9-12
**/
#include<iostream>
using namespace std;
//二叉树结构
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
//创建二叉树的结点
BinaryTreeNode* CreateBinaryTreeNode(int value);
//连接二叉树的结点
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight);
//打印二叉树的结点
void PrintTreeNode(BinaryTreeNode* pNode);
//先序遍历二叉树
void PrintTree(BinaryTreeNode* pRoot);
//二叉树销毁
void DestroyTree(BinaryTreeNode* pRoot);
//树的深度信息
int TreeDepth(BinaryTreeNode* pRoot);
//逐个判断每个结点的左右子树深度
bool IsBalanced_Solution1(BinaryTreeNode* pRoot);
bool IsBalanced(BinaryTreeNode* pRoot, int* pDepth);
bool IsBalanced_Solution2(BinaryTreeNode* pRoot);
int main()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
ConnectTreeNodes(pNode1, pNode2, pNode3);
ConnectTreeNodes(pNode2, pNode4, pNode5);
ConnectTreeNodes(pNode3, NULL, pNode6);
ConnectTreeNodes(pNode5, pNode7, NULL);
bool result=IsBalanced_Solution2(pNode1);
cout << "the result is: ";
if (result == true)
cout << "balance" << endl;
else
cout << "not balance" << endl;
system("PAUSE");
return 0;
}
//创建树结点
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->m_nValue = value;
pNode->m_pLeft = NULL;
pNode->m_pRight = NULL;
return pNode;
}
//连接树结点
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
if (pParent != NULL)
{
pParent->m_pLeft = pLeft;
pParent->m_pRight = pRight;
}
}
//打印
void PrintTreeNode(BinaryTreeNode* pNode)
{
if (pNode != NULL)
{
cout << "value of this node is: " << pNode->m_nValue << endl;
if (pNode->m_pLeft != NULL)
cout << "value of its left child is: " << pNode->m_pLeft->m_nValue << endl;
else
cout << "left child is null.\n";
if (pNode->m_pRight != NULL)
cout << "value of its right child is:" << pNode->m_pRight->m_nValue << endl;
else
cout << "right child is null.\n";
}
else
{
cout << "this node is null.\n";
}
cout << endl;
}
//先序遍历二叉树
void PrintTree(BinaryTreeNode* pRoot)
{
PrintTreeNode(pRoot);
if (pRoot != NULL)
{
if (pRoot->m_pLeft != NULL)
PrintTree(pRoot->m_pLeft);
if (pRoot->m_pRight != NULL)
PrintTree(pRoot->m_pRight);
}
}
//销毁
void DestroyTree(BinaryTreeNode* pRoot)
{
if (pRoot != NULL)
{
BinaryTreeNode* pLeft = pRoot->m_pLeft;
BinaryTreeNode* pRight = pRoot->m_pRight;
delete pRoot;
pRoot = NULL;
DestroyTree(pLeft);
DestroyTree(pRight);
}
}
//树深度
int TreeDepth(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return 0;
int nLeft = TreeDepth(pRoot->m_pLeft);
int nRight = TreeDepth(pRoot->m_pRight);
return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);
}
//逐个判断每个结点
bool IsBalanced_Solution1(BinaryTreeNode* pRoot)
{
if (pRoot == NULL)
return true;
int left = TreeDepth(pRoot->m_pLeft);
int right = TreeDepth(pRoot->m_pRight);
int diff = left - right;
if (diff > 1 || diff < -1)
return false;
return IsBalanced_Solution1(pRoot->m_pLeft)
&& IsBalanced_Solution1(pRoot->m_pRight);
}
//后序遍历的方法
bool IsBalanced_Solution2(BinaryTreeNode* pRoot)
{
int depth = 0;
return IsBalanced(pRoot, &depth);
}
bool IsBalanced(BinaryTreeNode* pRoot, int* pDepth)
{
if (pRoot == NULL)
{
*pDepth = 0;
return true;
}
int left, right;
if (IsBalanced(pRoot->m_pLeft, &left)
&& IsBalanced(pRoot->m_pRight, &right))
{
int diff = left - right;
if (diff <= 1 && diff >= -1)
{
*pDepth = 1 + (left > right ? left : right);
return true;
}
}
return false;
}