这个题目纠结了一会儿,终于从二叉树转化到AVL了。题目如下:

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

给一个二叉树判断它是不是平衡二叉树,根据平衡二叉树的定义:左右子树高度差的绝对值要小于1.那么我们在判断的时候就是拿到一个节点然后计算它两边左右子树的最大高度,判断这两者差的绝对值是否大于1就行。这样的话就得先拿递归写个判断树最大高度的函数,然后在判断函数里面调用就可以了。题解如下:

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int MaxDepth(TreeNode *temp)
{
if (temp == NULL)
return 0;
else
{
int aspros = MaxDepth(temp->left);
int defteros = MaxDepth(temp->right);
return 1 + (aspros>defteros ? aspros : defteros);
}
}
bool isBalanced(TreeNode *root)
{
if (root == NULL)
{
return true;
} int aspros = MaxDepth(root->left);
int defteros = MaxDepth(root->right); if (abs(aspros - defteros)>1)
return false;
// This way is wrong!
//if (abs(DepthTree(temp->left) - DepthTree(temp->right) > 1))
// return false;
else
return (isBalanced(root->left) && isBalanced(root->right));
}
};

这次学会了在递归中使用返回值。

[leetcode] 6. Balanced Binary Tree的更多相关文章

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

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

  2. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

  3. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  4. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  5. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  6. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  7. LeetCode之Balanced Binary Tree 平衡二叉树

    判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...

  8. LeetCode 110. Balanced Binary Tree (平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  9. 【LeetCode】Balanced Binary Tree(平衡二叉树)

    这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...

  10. Leetcode 110. Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

随机推荐

  1. vim配置之安装脚本

    vimConfig/install/install.sh git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle cp ...

  2. Mac安装appium 问题记录

    执行脚本报错:Xcode version [object Object] is not yet supported 原因:Xcode8以上的版本不支持Appium-1.5.3版本

  3. crs_register/crs_unregister 注册与移除RAC服务 --zhuanzai

    crs_register命令主要是将资源注册到CRS.该方法通常结合crs_stat -p 或者crs_profile先创建配置文件.同时crs_register也具有更新CRS的功能.本文将描述cr ...

  4. js实现的快速排序

    快速排序是一种平均性能非常优秀的排序算法,在很多场合都会应用到他. 了解快速排序于对开放高效率的软件有很重要的作用. 但是有不少的书本讲得并不是很清楚,而且不同的教材的实现方式也不尽相同, 我这里将最 ...

  5. ffmpeg默认输出中文为 UTF-8

    在使用ffmpeg 进行对音视频文件解码输出信息的时候会出现乱码. 从网上找到了说ffmpeg默认格式 为 utf-8 如果vs工程使用的的 Unicode 则需要将 utf-8转 Unicode 才 ...

  6. sceneManager.loadscene加载场景时不会主动去加载场景的依赖包,要手动加载或添加场景到build setting列表中

    假设有一场景1001.unity,,manifest文件如下: ManifestFileVersion: 0CRC: 425184873Hashes: AssetFileHash: serialize ...

  7. Git 仓库 SSH、HTTP、Gitweb (Nginx) 乱炖

    简介: 自己搭建 Git 仓库,实现 SSH 协议.配合 Nginx 实现 HTTP 协议拉取.推送代码. 利用 Nginx 实现 Gitweb 在线浏览代码,使用 Gitweb-theme 更新默认 ...

  8. 记一次结巴分词.net core 2.0版 nuget发布过程

    最近用到分词考虑很久,选用了结巴分词,原因见博客Lucene.net(4.8.0) 学习问题记录五: JIEba分词和Lucene的结合,以及对分词器的思考 既然选好了,难就开始行动吧 . 查了.ne ...

  9. Multithread之为什么spinlock必须是volatile?

    [Multithread之为什么spinlock必须是volatile?] 1.编译器的优化 在本次线程内,当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一个寄存器中:以后再取变量 ...

  10. python之time&datetime

    [time] secs:统一值,无local.UTC之分. struct_time:有local.UTC之分. time.time():返回secs,secs为统一值,无local&utc之分 ...