# 面试题39：平衡二叉树判断

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;
}``````
原文作者：平衡二叉树
原文地址: https://blog.csdn.net/kekong0713/article/details/52515917
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。