这个题目纠结了一会儿,终于从二叉树转化到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. Python基础之爬虫(持续更新中)

    python通过urllib.request.urlopen("https://www.baidu.com")访问网页 实战,去网站上下载一只猫的图片 import urllib. ...

  2. mysql connection phase(未整理)

    14.2.1 初始握手初始握手从服务器发送 Initial_Handshake_Packet开始.在这之后,客户端可以选择是否通过SSL_Connection_Request_Packet发送SSL连 ...

  3. [转]Jsp 与 JavaBean

    JavaBean 是一个遵循特定写法的 Java 类,它有以下特点: 1. Java 类具有一个无参的构造函数 2. 属性必须私有化. 3. 私有化的属性通过 public 类型的方法暴露给其它程序, ...

  4. Float IP设置

    浮动IP: 在做双机的时候,设定的一个IP,通过访问这个IP,具体到后台哪台机器,由系统指定. 浮动IP是随资源一起走的. 就是由软件根据具体情况把该IP设置在某一台机器上,对外提供服务 为了避免因为 ...

  5. 2018 Multi-University Training Contest 2-1007(hdu 6315)-题解

    一.题意 给定一个元素个数为$N(1 \le N \le 10^5)$初始序列$a$和$b$,$a$序列的初始值全为$0$,$b$序列的初始值为$1$到$N$的一个排列.有$T(1 \le T \le ...

  6. Windows 远程桌面连接Ubuntu16.04图像界面

    1.安装xrdp sudo apt-get install xrdp 2. 安装vnc4server sudo apt-get install vnc4server 3. 安装xubuntu-desk ...

  7. 「小程序JAVA实战」小程序视频处理工具ffmpeg(47)

    转自:https://idig8.com/2018/09/16/xiaochengxujavashizhanxiaochengxushipinchuligongjuffmpeg46/ 前面已经把视频成 ...

  8. Spring Boot实践——多线程

    多线程 Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程.使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor.而实际开发中任务一 ...

  9. libcurl 调用curl_easy_getinfo( ) 返回错误码对照

    //执行设置好的操作 res = curl_easy_perform(easy_handle); //获取HTTP错误码 ; curl_easy_getinfo(easy_handle, CURLIN ...

  10. 有单例模式 Singleton 涉及的一些防止类被继承的东西

    c#中 : ------------------------------- 当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承. java中: ------------------ ...