/*************************************************************************
> File Name: 37_TreeDepth.cpp
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: 2016年09月03日 星期六 09时49分38秒
************************************************************************/ #include <stdio.h>
#include <malloc.h>
#include <math.h> // 二叉树结构体
struct BinaryTree
{
int val;
struct BinaryTree* left;
struct BinaryTree* right;
}; BinaryTree* createTree()
{
BinaryTree* root = (BinaryTree*)malloc(sizeof(BinaryTree));
root->val = ;
root->left = (BinaryTree*)malloc(sizeof(BinaryTree));
root->left->val = ;
root->right = (BinaryTree*)malloc(sizeof(BinaryTree));
root->right->val = ;
root->left->left = (BinaryTree*)malloc(sizeof(BinaryTree));
root->left->left->val = ;
root->left->left->left = NULL;
root->left->left->right = NULL;
root->left->right = (BinaryTree*)malloc(sizeof(BinaryTree));
root->left->right->val = ;
root->left->right->right = NULL;
root->left->right->left = (BinaryTree*)malloc(sizeof(BinaryTree));
root->left->right->left->val = ;
root->left->right->left->left = NULL;
root->left->right->left->right = NULL;
root->right->right = (BinaryTree*)malloc(sizeof(BinaryTree));
root->right->right->val = ;
root->right->left = NULL;
root->right->right->left = NULL;
root->right->right->right = NULL;
// root->right->right->right = (BinaryTree*)malloc(sizeof(BinaryTree));
// root->right->right->right->val = 8;
// root->right->right->right->left = root->right->right->right->right = NULL; return root;
} // 二叉树中序遍历
void InOrder(BinaryTree* root)
{
if (root == NULL)
return; InOrder(root->left);
printf("%d ", root->val);
InOrder(root->right);
} // 二叉树的深度
int TreeDepth(BinaryTree* root)
{
if (root == NULL)
return ; int left = TreeDepth(root->left);
int right = TreeDepth(root->right); return (left>right) ? (left+) : (right+);
} // 判断一棵树是不是平衡二叉树
bool isBalanced(BinaryTree* root)
{
if (root == NULL)
return true; int left = TreeDepth(root->left);
int right = TreeDepth(root->right); int diff = abs(left - right);
if (diff > )
return false; return isBalanced(root->left) && isBalanced(root->right);
} // 后序遍历方法
bool isBalanced2(BinaryTree* root, int* depth)
{
if (root == NULL)
{
*depth = ;
return true;
}
int left, right;
if (isBalanced2(root->left, &left) && isBalanced2(root->right, &right))
{
int diff = abs(left - right);
if (diff <= )
{
*depth = + (left>right ? left : right);
return true;
}
}
return false;
} bool isBalanced2(BinaryTree* root)
{
int depth = ;
return isBalanced2(root, &depth);
} int main()
{
BinaryTree* test = createTree();
InOrder(test);
int depth = TreeDepth(test);
printf("\ndepth is %d\n", depth);
if (isBalanced2(test) == true)
printf("Balance\n");
else
printf("No Balance\n"); }

剑指Offer37 二叉树深度与平衡二叉树判断的更多相关文章

  1. 剑指Offer-37.二叉树的深度(C++/Java)

    题目: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 分析: 递归求解左右子树的最大值即可,每遍历到一个结点,深度加1,最后 ...

  2. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  3. 剑指Offer——二叉树

    剑指Offer--二叉树 前言 数据结构通常是编程面试中考察的重点.在参加面试之前,应聘者需要熟练掌握链表.树.栈.队列和哈希表等数据结构,以及它们的操作.本片博文主要讲解二叉树操作的相关知识,主要包 ...

  4. JS数据结构与算法 - 剑指offer二叉树算法题汇总

    ❗❗ 必看经验 在博主刷题期间,基本上是碰到一道二叉树就不会碰到一道就不会,有时候一个下午都在搞一道题,看别人解题思路就算能看懂,自己写就呵呵了.一气之下不刷了,改而先去把二叉树的基础算法给搞搞懂,然 ...

  5. 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)

    从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...

  6. 【剑指Offer】39、平衡二叉树

      题目描述:   输入一棵二叉树,判断该二叉树是否是平衡二叉树.这里的定义是:如果某二叉树中任意结点的左.右子树的深度相差不超过1,那么它就是一棵平衡二叉树.   解题思路:   首先对于本题我们要 ...

  7. 剑指offer 二叉树中和为某一个值的路径

    剑指offer 牛客网 二叉树中和为某一个值的路径 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 15:53:58 2 ...

  8. 剑指offer 二叉树的层序遍历

    剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...

  9. 剑指offer(39)平衡二叉树

    题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 题目分析 第一种方法: 正常思路,应该会获得节点的左子树和右子树的高度,然后比较高度差是否小于1. 可是这样有一个问题,就是节点重复遍历了,影 ...

随机推荐

  1. ASP.NET基础之HttpModule 、HttpContext、 HttpHandler

    http://www.cnblogs.com/wujy/p/3261141.html http://www.cnblogs.com/wujy/p/3264475.html http://www.cnb ...

  2. javascript数据缓存

    if(!self.hotCityPrice[city]) { $.ajax({ type: 'GET', url: self.hotCityUrl, data: {cityCode: city, t: ...

  3. Django官方文档学习1——第一个helloworld页面

    Django 1.10官方文档:https://docs.djangoproject.com/en/1.10/intro/tutorial01/ 1.查看django版本 python -m djan ...

  4. Php最近1个月总结

    1.数据库方面太薄弱. 2.对于Php性能的调优也没有用到很专业的工具. 3.大型网站的架构也没有一个概念,需要细致的了解.

  5. 终于可以发布Delphi下收点OSGI精髓皮毛而设计的插件框架WisdomPluginFramework

    这是一个Delphi实现的插件框架,我耗费了相当相当相当多的精力来设计她,虽然仅闪着点我微薄智慧的光芒,但我还是决定用Wisdom来命名她,也因它是我绝无仅有的,在完成1年多后回头来看仍能相当满意的作 ...

  6. Android多线程研究(1)——线程基础及源代码剖析

    从今天起我们来看一下Android中的多线程的知识,Android入门easy,可是要完毕一个完好的产品却不easy,让我们从线程開始一步步深入Android内部. 一.线程基础回想 package ...

  7. 理解reserve与resize

    1.首先明白capacity与size的概念,capacity表示当前可以容纳多少个元素,size表示当前有多少个元素.为了避免频繁地分配内存,vector预留了一些内存.也就是说:size<= ...

  8. QML学习笔记之三

    import QtQuick 1.1 Row{ spacing:2 Rectangle{color:"red";width:50;height:50} Rectangle{colo ...

  9. 转换到 COFF 期间失败: 文件无效或损坏 解决方法

    转自csdn 终极解决方案:VS2010在经历一些更新后,建立Win32 Console Project时会出“error LNK1123” 错误,解决方案为将 项目|项目属性|配置属性|清单工具|输 ...

  10. UIBezierPathStudyDemo

    import UIKit import XCPlayground //创建view let myView = UIView(frame:CGRectMake(0, 0, 300, 200)) //实时 ...