/*************************************************************************
> 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. 通过SimpleAction显示一个listview

    private void simpleAction1_Execute(object sender, SimpleActionExecuteEventArgs e) { IObjectSpace os ...

  2. JavaScript的因为所以

    各位看官,楼主开始说过写几篇博客,这是这个系列的最后一集.吾以为:了解JavaScript的身世之谜,掌握其近乎心想事成的变量系统,了解其解析运行的偷梁换柱之法,熟悉布大师迂回曲折的OOP实现.那你离 ...

  3. hdu 2821 Pusher (dfs)

    把这个写出来是不是就意味着把   http://www.hacker.org/push  这个游戏打爆了? ~啊哈哈哈 其实只要找到一个就可以退出了  所以效率也不算很低的  可以直接DFS呀呀呀呀 ...

  4. c# implicit explicit关键字(隐式和显式数据类型转换)

    implicit关键字用于声明隐式的用户定义类型转换运算符.(explicit反之)explicit则用于显示转换用户自定义类型.static implicit operator target_typ ...

  5. JSP+Servlet+JavaBean

    MVC是三个单词的缩写:M,Model(模型):V,View( 视图 ),C,Control(控制). MVC模式的目的就是实现Web系统的职能分工, Model层:实现系统的业务逻辑,即javaBe ...

  6. STM32 DFU -- Device Firmware Upgrade

    DFU Class Requests Get Status The Host employs the DFU_GETSTATUS request to facilitate synchronizati ...

  7. ASP.NET Web Forms的改进

    虽然ASP.NET Web Forms不是vNext计划的一部分,但它并没有被忽视.作为Visual Studio 2013 Update 2的一部分,它重新开始支持新工具.EF集成和Roslyn. ...

  8. Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列

    B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...

  9. 一步步学Mybatis-实现单表情况下的CRUD操作 (3)

    今天这一章要紧接上一讲中的东西,本章中创建基于单表操作的CRUD与GetList操作,此示例中以Visitor表为范例,为了创建一点测试数据我们先弄个Add方法吧 继续在上次的IVisitorOper ...

  10. Azure编程笔记(1):序列化复杂类型的TableEntity字段

    内容提要 在使用MicrosoftAzure的CloudTable存储数据时,我们先要把数据定义成TableEntity的子类.假设TableEntity中包括复杂类型(比方容器类型如List等.或者 ...