剑指Offer37 二叉树深度与平衡二叉树判断
/*************************************************************************
> 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 二叉树深度与平衡二叉树判断的更多相关文章
- 剑指Offer-37.二叉树的深度(C++/Java)
题目: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 分析: 递归求解左右子树的最大值即可,每遍历到一个结点,深度加1,最后 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 剑指Offer——二叉树
剑指Offer--二叉树 前言 数据结构通常是编程面试中考察的重点.在参加面试之前,应聘者需要熟练掌握链表.树.栈.队列和哈希表等数据结构,以及它们的操作.本片博文主要讲解二叉树操作的相关知识,主要包 ...
- JS数据结构与算法 - 剑指offer二叉树算法题汇总
❗❗ 必看经验 在博主刷题期间,基本上是碰到一道二叉树就不会碰到一道就不会,有时候一个下午都在搞一道题,看别人解题思路就算能看懂,自己写就呵呵了.一气之下不刷了,改而先去把二叉树的基础算法给搞搞懂,然 ...
- 剑指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 ...
- 【剑指Offer】39、平衡二叉树
题目描述: 输入一棵二叉树,判断该二叉树是否是平衡二叉树.这里的定义是:如果某二叉树中任意结点的左.右子树的深度相差不超过1,那么它就是一棵平衡二叉树. 解题思路: 首先对于本题我们要 ...
- 剑指offer 二叉树中和为某一个值的路径
剑指offer 牛客网 二叉树中和为某一个值的路径 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 15:53:58 2 ...
- 剑指offer 二叉树的层序遍历
剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...
- 剑指offer(39)平衡二叉树
题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 题目分析 第一种方法: 正常思路,应该会获得节点的左子树和右子树的高度,然后比较高度差是否小于1. 可是这样有一个问题,就是节点重复遍历了,影 ...
随机推荐
- 关于刘冬大侠Spring.NET系列学习笔记3的一点勘正
诚如他第22楼“只因渴求等待”提出的疑问一样,他的下面那一段代码是存在一点点问题的, XElement root = XElement.Load(fileName); var objects = fr ...
- c#实现word,winWordControl 文档不允许复制、粘贴、隐藏工具栏、快捷保存
1.隐藏工具栏 //隐藏工具栏 ; i <= winWordControl1.document.CommandBars.Count; i++) { winWordControl1.documen ...
- ASP.net 服务器监控
参考代码: 1,页面 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SMP ...
- wikioi 1098 均分纸牌
题目描述 Description 有 N 堆纸牌,编号分别为 1,2,-, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若于张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸 ...
- 以Outlook样式分组和排列数据项
转载:http://www.cnblogs.com/peterzb/archive/2009/05/29/1491781.html OutlookGrid:以Outlook样式分组和排列数据项 (这里 ...
- Failed to issue method call: Unit mysqld.service failed to load: No such file or directory.
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- javascript、jquery获取网页的高度和宽度
javascript: 可视区域宽 :document.documentElement.clientWidth (width + padding) 可视区域高 :document.documentE ...
- DCEF3 相关资料
DCEF3 调用 js http://www.cnblogs.com/Delphi-Farmer/p/4103708.html interface uses ceflib;//其它 type //这里 ...
- C++之vector中元素删除
今天在删除vector中的元素中遇到一个问题,这里记录下来以便以后查阅. 预备知识:用到了erase()函数,对于一个容器c来说,假设迭代器为p,那么执行: c.erase(p)之后就删除了容器c中p ...
- 【Python笔记】图片处理库PIL的源代码安装步骤
前段时间项目须要对某些图片打水印,用到Python的PIL库,本文以Imaging-1.1.7为例,记录PIL库的源代码编译/安装步骤. PIL全称Python Image Library.它支持多种 ...