您的位置:首页 > Web前端

剑指offer 39-二叉树的深度 判断二叉树是否为平衡二叉树

2015-06-25 17:09 711 查看
方法1:得到左右子树的深度,判断

方法2:存储节点深度,边遍历边判断

#include<iostream>
using namespace std;

struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};

BinaryTreeNode* CreateBinaryTreeNode(int value)
{
BinaryTreeNode* node = new BinaryTreeNode();
node->m_nValue = value;
node->m_pLeft=node->m_pRight =NULL;
return node;
}

void ConnectTreeNodes(BinaryTreeNode* head,BinaryTreeNode* left ,BinaryTreeNode* right)
{
if(head!=NULL)
{
head->m_pLeft=left;
head->m_pRight=right;
}
}
void printBinaryTree(BinaryTreeNode* head)
{
if(head==NULL)
return;
printBinaryTree(head->m_pLeft);
cout<<head->m_nValue;
printBinaryTree(head->m_pRight);
}

void DestroyTree(BinaryTreeNode* pRoot)
{
if(pRoot!=NULL)
{
BinaryTreeNode* left = pRoot->m_pLeft;
BinaryTreeNode* right = pRoot->m_pRight;
delete pRoot;
pRoot=NULL;
DestroyTree(left);
DestroyTree(right);
}
}
//树的深度
int TreeDepth(BinaryTreeNode* pRoot)
{
if(pRoot==NULL)
return 0;
int left = TreeDepth(pRoot->m_pLeft);
int right = TreeDepth(pRoot->m_pRight);

return 1+(left>right?left:right);
}
//方法1
bool IsBalanced(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(pRoot->m_pLeft) && IsBalanced(pRoot->m_pRight));
}
//方法2
bool IsBalanced(BinaryTreeNode* pRoot,int* pDepth)
{
if(pRoot==NULL)
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;
}

bool IsBalanced2(BinaryTreeNode* pRoot)
{
int depth = 0;
return IsBalanced(pRoot,&depth);
}
// ====================测试代码====================
void Test(char *testName,BinaryTreeNode* pRoot, bool expected)
{
cout<<testName<<endl;
if(expected == IsBalanced(pRoot))
printf("Solutions1: passed.\n");
else
printf("Solutions2: failed.\n");

if(expected == IsBalanced2(pRoot))
printf("Solutions1: passed.\n");
else
printf("Solutions2: failed.\n");

}

// 1
// / \
// 2 3
// /\ \
// 4 5 6
// /
// 7
void Test1()
{
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);

Test("test1",pNode1, true);

DestroyTree(pNode1);
}

// 1
// /
// 2
// /
// 3
// /
// 4
// /
// 5
void Test2()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);

ConnectTreeNodes(pNode1, pNode2, NULL);
ConnectTreeNodes(pNode2, pNode3, NULL);
ConnectTreeNodes(pNode3, pNode4, NULL);
ConnectTreeNodes(pNode4, pNode5, NULL);

Test("test2",pNode1, false);
DestroyTree(pNode1);
}

// 1
// \
// 2
// \
// 3
// \
// 4
// \
// 5
void Test3()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);

ConnectTreeNodes(pNode1, NULL, pNode2);
ConnectTreeNodes(pNode2, NULL, pNode3);
ConnectTreeNodes(pNode3, NULL, pNode4);
ConnectTreeNodes(pNode4, NULL, pNode5);

Test("test3",pNode1, false);

DestroyTree(pNode1);
}

// 树中只有1个结点
void Test4()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
Test("test4",pNode1, true);

DestroyTree(pNode1);
}

// 树中没有结点
void Test5()
{
Test("test5",NULL, true);
}

int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: