这个题目纠结了一会儿,终于从二叉树转化到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. PAT 甲级 1008 Elevator (20)(20 分)模拟水题

    题目翻译: 1008.电梯 在我们的城市里,最高的建筑物里只有一部电梯.有一份由N个正数组成的请求列表.这些数表示电梯将会以规定的顺序在哪些楼层停下.电梯升高一层需要6秒,下降一层需要4秒.每次停下电 ...

  2. Bootstrap-CL:Well

    ylbtech-Bootstrap-CL:Well 1.返回顶部 1. Bootstrap Well Well 是一种会引起内容凹陷显示或插图效果的容器 <div>.为了创建 Well,只 ...

  3. SPM——How to use github

    In this semester, we take a class called 'Software Project Management'. And in this class, we have l ...

  4. ES6系列_7之箭头函数和扩展

    1.默认值 在ES6中给我们增加了默认值的操作相关代码如下: function add(a,b=1){ return a+b; } console.log(add(1)); 可以看到现在只需要传递一个 ...

  5. 496. Next Greater Element I + 503. Next Greater Element II + 556. Next Greater Element III

    ▶ 给定一个数组与它的一个子列,对于数组中的一个元素,定义它右边第一个比他大的元素称为他的后继,求所给子列的后继构成的数组 ▶ 第 496 题,规定数组最后一个元素即数组最大元素的后继均为 -1 ● ...

  6. vs2010 出现“未能将 ProteusDebugEngine 调试器附加到计算机”

    vs2010 打开项目时出现如下图错误,解决方法: 1.查看C:\Progream Files下的Internet Explorer文件夹还在不在,不在则会出现此问题: 2.可以右键项目属性-调试-勾 ...

  7. MobaXterm 错行,乱码

    最近使用MobaXterm ssh,发现进入ssh显示中文没问题.但如果用VIM编辑文件时如果有中文,修改插入操作有时就会有串行乱码情况,改vim编码也不行.都没有完美解决.最后发现只需要在MobaX ...

  8. js中元素结点的引用

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. 初步认识cookie

    cookie是由服务器创建,客户端读取及保存它的 同类请求指的是资源路径相同 Cookie的默认路径绑定是所请求的资源路径绑定的 ,指定路径时必须要有项目名称(说明是哪个项目) 使用cookie时还要 ...

  10. hue database is locked

    hue使用mysql作为元数据库 hue默认使用sqlite作为元数据库,不推荐在生产环境中使用.会经常出现database is lock的问题. 解决方法: 其实官网也有解决方法,不过过程似乎有点 ...