《剑指offer》第五十五题(平衡二叉树)
// 面试题55(二):平衡二叉树
// 题目:输入一棵二叉树的根结点,判断该树是不是平衡二叉树。如果某二叉树中
// 任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。 #include <iostream>
#include "BinaryTree.h" // ====================方法1====================
//迭代的从上到下,判断每个节点是否是平衡树,会导致一个节点的深度重复计算 int TreeDepth(const BinaryTreeNode* pRoot)//检测节点深度
{
if (pRoot == nullptr)
return ; int nLeft = TreeDepth(pRoot->m_pLeft);
int nRight = TreeDepth(pRoot->m_pRight); return (nLeft > nRight) ? (nLeft + ) : (nRight + );
} bool IsBalanced_Solution1(const BinaryTreeNode* pRoot)//记录
{
if (pRoot == nullptr)
return true; int left = TreeDepth(pRoot->m_pLeft);
int right = TreeDepth(pRoot->m_pRight);
int diff = left - right;
if (diff > || diff < -)
return false; return IsBalanced_Solution1(pRoot->m_pLeft)
&& IsBalanced_Solution1(pRoot->m_pRight);
} // ====================方法2====================
//从下到上检测,如果节点是平衡树,就记录其深度,每个节点被计算一次
bool IsBalanced(const BinaryTreeNode* pRoot, int* pDepth); bool IsBalanced_Solution2(const BinaryTreeNode* pRoot)
{
int depth = ;
return IsBalanced(pRoot, &depth);
} bool IsBalanced(const BinaryTreeNode* pRoot, int* pDepth)
{
if (pRoot == nullptr)
{
*pDepth = ;
return true;
} int left, right;
if (IsBalanced(pRoot->m_pLeft, &left)
&& IsBalanced(pRoot->m_pRight, &right))//if条件是找到子节点开始从下向上判断节点是不是平衡树
{
int diff = left - right;
if (diff <= && diff >= -)//如果是,记录最大深度
{
*pDepth = + (left > right ? left : right);
return true;
}
} return false;
} // ====================测试代码====================
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();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode1, pNode2, pNode3);
ConnectTreeNodes(pNode2, pNode4, pNode5);
ConnectTreeNodes(pNode3, pNode6, pNode7); Test("Test1", pNode1, true); DestroyTree(pNode1);
} // 不是完全二叉树,但是平衡二叉树
// 1
// / \
// 2 3
// /\ \
// 4 5 6
// /
//
void Test2()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(); 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
// /
//
void Test3()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode1, pNode2, pNode3);
ConnectTreeNodes(pNode2, pNode4, pNode5);
ConnectTreeNodes(pNode5, pNode6, nullptr); Test("Test3", pNode1, false); DestroyTree(pNode1);
} // 1
// /
// 2
// /
// 3
// /
// 4
// /
//
void Test4()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(); 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
// \
//
void Test5()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(); 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();
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();
system("pause");
return ;
}
《剑指offer》第五十五题(平衡二叉树)的更多相关文章
- 剑指Offer(二十五):复杂链表的复制
剑指Offer(二十五):复杂链表的复制 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/bai ...
- 剑指Offer(三十五):数组中的逆序对
剑指Offer(三十五):数组中的逆序对 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/bai ...
- 《剑指offer》第二十五题(合并两个排序的链表)
// 面试题25:合并两个排序的链表 // 题目:输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按 // 照递增排序的.例如输入图3.11中的链表1和链表2,则合并之后的升序链表如链 ...
- 《剑指offer》第十五题(二进制中1的个数)
// 面试题:二进制中1的个数 // 题目:请实现一个函数,输入一个整数,输出该数二进制表示中1的个数.例如 // 把9表示成二进制是1001,有2位是1.因此如果输入9,该函数输出2. #inclu ...
- 《剑指offer》第二十六题(树的子结构)
// 面试题26:树的子结构 // 题目:输入两棵二叉树A和B,判断B是不是A的子结构. #include <iostream> struct BinaryTreeNode { doubl ...
- 《剑指offer》第十九题(正则表达式匹配)
// 面试题19:正则表达式匹配 // 题目:请实现一个函数用来匹配包含'.'和'*'的正则表达式.模式中的字符'.' // 表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次).在本题 ...
- 《剑指offer》第二十九题(顺时针打印矩阵)
// 面试题29:顺时针打印矩阵 // 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. #include <iostream> void PrintMatrixInC ...
- 《剑指offer》第二十八题(对称的二叉树)
// 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...
- 《剑指offer》第二十四题(反转链表)
// 面试题24:反转链表 // 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的 // 头结点. #include <iostream> #include &quo ...
- 《剑指offer》第二十二题(链表中倒数第k个结点)
// 面试题22:链表中倒数第k个结点 // 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯, // 本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点, ...
随机推荐
- Eloquent JavaScript #06# class
索引 Notes this Prototype 类 class符号 覆盖派生属性 Maps Symbols iterator接口 Getters, setters, and statics 继承 in ...
- pytorch种, 一维Conv1d, 二维Conv2d
pytorch之nn.Conv1d详解 之前学习pytorch用于文本分类的时候,用到了一维卷积,花了点时间了解其中的原理,看网上也没有详细解释的博客,所以就记录一下. Conv1dclass tor ...
- django 表单常用field
BooleanField字段:相当于单选框 CharField:接受字符串 参数:max_length最大长度,min_length最小长度 require字段是否是必须的,默认为required=T ...
- bzoj3932 / P3168 [CQOI2015]任务查询系统(主席树+差分)
P3168 [CQOI2015]任务查询系统 看到第k小,就是主席树辣 对于每一段任务(a,b,k),在版本a的主席树+k,版本b+1的主席树-k 同一时间可能有多次修改,所以开个vector存操作, ...
- netty支持的各种socketchannel实现及比较
性能上从低到高如下: OioSocketChannel:传统,阻塞式编程. NioSocketChannel:select/poll或者epoll,jdk 7之后linux下会自动选择epoll. E ...
- 第五章 CSS常用属性笔记
1. span标签 突显,强调局部文字的作用. 2.字体样式 font-size: 字体大小 font-style:normal,italic(倾斜) font-weight:normal,bold( ...
- 关于A left join B,A是否一定是主表?
一般情况,我们作左连接 select * from A left join B on A.id=B.a_id;一定认为A就是主表,其实还有另外的情况,我们若将sql改写成 select * from ...
- 20145308 《网络对抗》 MSF基础应用 学习总结
20145308 <网络对抗> MSF基础应用 学习总结 实验内容 掌握metasploit的基本应用方式,掌握常用的三种攻击方式的思路.具体需要完成(1)一个主动攻击,如ms08_067 ...
- STL相关问题
写set容器遇到以下问题: C:\Users\admin\Desktop\未命名2.cpp In function 'int main()': 67 98 C:\Users\admin\Desktop ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...