题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1。

思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决。

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool ans;//答案会记录在此。
int DFS(TreeNode* root)
{
if(!root || !ans) return ; //已经错了,不必要再比较下去
int b=DFS(root->right);
int a=DFS(root->left);
if(abs(a - b)>) ans=false;
return max(a,b)+;
} bool isBalanced(TreeNode* root) {
ans=true;//初始化答案
DFS(root);
return ans;
}
};

AC代码

LeetCode Balanced Binary Tree (判断平衡树)的更多相关文章

  1. LeetCode: Balanced Binary Tree 解题报告

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

  2. LeetCode——Balanced Binary Tree(判断是否平衡二叉树)

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

  3. balanced binary tree(判断是否是平衡二叉树)

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

  4. [leetcode]Balanced Binary Tree @ Python

    原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...

  5. LeetCode——Balanced Binary Tree

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

  6. [Leetcode] Balanced binary tree平衡二叉树

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

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

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

  8. LeetCode - Balanced Binary Tree

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

  9. 【easy】110. Balanced Binary Tree判断二叉树是否平衡

    判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...

随机推荐

  1. hdu 4622 Reincarnation SAM模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有Q次区间查询(Q <= 10000),问区 ...

  2. butterknife7.0.1使用

    1.官网:http://jakewharton.github.io/butterknife/ Introduction Annotate fields with @Bind and a view ID ...

  3. Objective-C中的数据类型、常量、变量、运算符与表达式

    1.Objective-C中的数据类型: Objective-C中的基本数据类型有:int.char(-128-127).float.double.BOOL,Byte(0-255) Id类型相当于(等 ...

  4. 【BZOJ2428】[HAOI2006]均分数据

    Description 已知N个正整数:A1.A2.…….An .今要将它们分成M组,使得各组数据的数值和最平均,即各组的均方差最小.均方差公式如下: ,其中σ为均方差,是各组数据和的平均值,xi为第 ...

  5. 微软职位内部推荐-Principal DEV Manager for Bing Client

    微软近期Open的职位: Title: Principal DEV Manager for Bing ClientGroup: Search Technology Center Asia, BingW ...

  6. iOS runtime 与 runloop

    runtime是运行时机制,就是运行到的时候才会执行的机制(类似于栏加载)属于时间先后性质的 runloop 是主线程的运行的意思   属于 (loop) 循环性质的 以下是 具体点的 解释: run ...

  7. 如何把由js生成的内容水平居中?

    一.问题来源 如右侧的微博关注组件,直接用div的align居中没效果,我就开始百度了.即在<div> <script>.....</script>  </d ...

  8. uva 1103

    弄懂题意后  其实就是一个dfs /************************************************************************* > Aut ...

  9. 关于PHP写APP接口的安全问题探讨(一)

    在探讨这个问题之前,先要确认一点的是,作为一名互联网Coder,无论你是前端或者后端你都要对http请求要有一定的了解,知道http特性,要清楚的了解http里面的Request与Response是什 ...

  10. mybatis和spring3.1整合

    因spring3发布时mybatis还没有出正式版本,所以spring没有整合最新的mybatis.不过社区倒是开发了一个中间件. 需要的jar包 mybatis-3.0.6.jar mybatis- ...