// 面试题55(一):二叉树的深度
// 题目:输入一棵二叉树的根结点,求该树的深度。从根结点到叶结点依次经过的
// 结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
#include <cstdio>
int TreeDepth(const BinaryTreeNode* pRoot)
{
if(pRoot==nullptr)
return 0;
//返回左右节点的长度,哪个长哪个加1
int nLeft=TreeDepth(pRoot->m_pLeft);//左树的深度
int nRight=TreeDepth(pRoot->m_pRIght);
return (nLeft>nRight)?(nLeft+1):(nRight+1);
}
// ====================测试代码====================
void Test(const char* testName, const BinaryTreeNode* pRoot, int expected)
{
int result = TreeDepth(pRoot);
if(expected == result)
printf("%s passed.n", testName);
else
printf("%s FAILED.n", testName);
}
// 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, nullptr, pNode6);
ConnectTreeNodes(pNode5, pNode7, nullptr);
Test("Test1", pNode1, 4);
DestroyTree(pNode1);
}
// 1
// /
// 面试题55(二):平衡二叉树
// 题目:输入一棵二叉树的根结点,判断该树是不是平衡二叉树。如果某二叉树中
// 任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。
#include <cstdio>
bool IsBalanced(const BinaryTreeNode* pRoot)
{
if(pRoot==nullptr)
return 0;
int nLeft=TreeDepth(pRoot->m_pLeft);
int nRight=TreeDepth(pRoot->m_pRight);
//比较左右差值大于1?
int diff=nLeft-nRight;
if(diff>1||diff<-1)
return false;
return IsBalanced(pRoot->m_pLeft) && IsBalanced(pRoot->m_pRight);
}
//上述代码会出现重复的检测,重复遍历同一节点会影响性能
//后序遍历LRN,在遍历一个节点之前就已经遍历了它的左右子树。在遍历每个节点的时候记录深度
bool IsBalanced(BInaryTree* pRoot,int pDepth)
{
if(pRoot==nullptr)
{
*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;
}
bool IsBalanced_Solution2(const BinaryTreeNode* pRoot)
{
int depth = 0;
return IsBalanced(pRoot, &depth);
}
// ====================测试代码====================
void Test(const char* testName, const BinaryTreeNode* pRoot, bool expected)
{
if(testName != nullptr)
printf("%s begins:n", testName);
printf("Solution1 begins: ");
if(IsBalanced_Solution1(pRoot) == expected)
printf("Passed.n");
else
printf("Failed.n");
printf("Solution2 begins: ");
if(IsBalanced_Solution2(pRoot) == expected)
printf("Passed.n");
else
printf("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, pNode6, pNode7);
Test("Test1", pNode1, true);
DestroyTree(pNode1);
}
// 不是完全二叉树,但是平衡二叉树
// 1
// /
// 2 3
// /
// 4 5 6
// /
// 7
void Test2()
{
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, nullptr, pNode6);
ConnectTreeNodes(pNode5, pNode7, nullptr);
Test("Test2", pNode1, true);
DestroyTree(pNode1);
}
// 不是平衡二叉树
// 1
// /
// 2 3
// /
// 4 5
// /
// 6
void Test3()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
ConnectTreeNodes(pNode1, pNode2, pNode3);
ConnectTreeNodes(pNode2, pNode4, pNode5);
ConnectTreeNodes(pNode5, pNode6, nullptr);
Test("Test3", pNode1, false);
DestroyTree(pNode1);
}
// 1
// /
// 2
// /
// 3
// /
// 4
// /
// 5
void Test4()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
ConnectTreeNodes(pNode1, pNode2, nullptr);
ConnectTreeNodes(pNode2, pNode3, nullptr);
ConnectTreeNodes(pNode3, pNode4, nullptr);
ConnectTreeNodes(pNode4, pNode5, nullptr);
Test("Test4", pNode1, false);
DestroyTree(pNode1);
}
// 1
//
// 2
//
// 3
//
// 4
//
// 5
void Test5()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
ConnectTreeNodes(pNode1, nullptr, pNode2);
ConnectTreeNodes(pNode2, nullptr, pNode3);
ConnectTreeNodes(pNode3, nullptr, pNode4);
ConnectTreeNodes(pNode4, nullptr, pNode5);
Test("Test5", pNode1, false);
DestroyTree(pNode1);
}
// 树中只有1个结点
void Test6()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
Test("Test6", pNode1, true);
DestroyTree(pNode1);
}
// 树中没有结点
void Test7()
{
Test("Test7", nullptr, true);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
return 0;
}