剑指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;
}
方法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;
}
相关文章推荐
- css hack
- jquery的post方法 使用从后台获取回来的数据
- AngularJs 用户登录验证模块(demo)参考总结
- javascript放大镜效果
- 【JS】根据表格ID进行排序(附注释)
- html+css的高级盒子模型
- 打包文件 MANIFEST.MF 功能详解
- Java - How to write Thread-Safe Code in Java
- JavaScript SUM and GROUP BY of JSON data
- jqery和js如何判断checkbox是否选中 (转)
- 读书时间《JavaScript高级程序设计》二:面向对象
- How to SUM and GROUP BY of JSON data?
- 单独js中el表达式的执行
- JS event使用方法详解
- js返回上一页并刷新的多种实现方法
- 窗口大小调整后处理事件jQuery插件ResizeEnd
- LintCode Nth to Last Node in List 链表倒数第n个节点
- ReactiveCocoa入门教程——第二部分
- MVC Razor语法的一点研究(关于 @Html.ValidationSummary("",new { @class = "text-danger" }))
- java中Scanner输入字符串和BufferedReader输入字符串的区别