《剑指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个结点, ...
随机推荐
- navicat mysql导出数据 批量插入的形式
这里介绍的是mysql 相同服务器类型数据传输的高级设置 选中数据库后右键“ 转储SQL文件”默认导出的记录格式是一条条的,采用的是”完整插入语句”,格式如下 '); '); '); 这种格式保证了兼 ...
- python的zipfile、tarfile模块
zipfile.tarfile的用法 先占个位置,后续再实际操作和补充 参考 https://www.cnblogs.com/MnCu8261/p/5494807.html
- oracle 11g enq: JI – contention等待事件
最近使用物化视图同步的环境在大量刷新的时候频繁出现enq: JI – contention等待事件,经查: JI enqueue is acquired in exclusive mode on th ...
- Angular routing生成路由和路由的跳转
Angular routing生成路由和路由的跳转 什么是路由 路由的目的是可以让根组件按照不同的需求动态加载不同的组件. 根据不同地址,加载不同组件,实现单页面应用. Angular 命令创建一个配 ...
- Python3 tkinter基础 Canvas coords 移动直线,itemconfig 设置矩形的颜色, delete 删除一条直线
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Spring Cloud 入门指南
Spring Cloud 方志朋-史上最简单的 Spring Cloud 教程
- Python 打包中 setpy.py settuptools pbr 的了解
背景 nova服务构建失败,报错: 'tests_require' must be a string or list of strings containing valid project/versi ...
- ubuntu upgrade
升级命令 虽然 apt-get 经常被人诟病,但实际上它还是个挺好用的软件包管理器.在 Ubuntu 14.04 以后的系统中,apt-get 相关的升级更新命令有四个: apt-get update ...
- 为什么不能用drop function add 去删除函数? 因为不能使用 mysql中的保留字!
mysql中有很多的 保留字, 也叫关键字, 你在使用 数据库中的任何东西, 都最好是 避开这些关键字/保留字, 包括 数据库名, 表名, 字段名, 函数名, 存储过程名. 这些关键字包括: mysq ...
- 使用msi自动安装系统
在实际生活中, 还是要尽量使用 自动化 脚本 等来处理/执行问题, 那样更快更省力省时间 要多使用 网络工具, 网络工具在 管理/ 使用网络的过程 中还是很有用的. 要有这种 "多使用网络工 ...