问题

给出一棵二叉树,判断它是否在高度上是平衡的。

对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于1的一棵二叉树。

初始思路

根据定义,思路应该比较直接:递归计算每个节点左右子树的深度,只要发现一次深度差大于1的情况,即可终止递归返回不平衡的结果。最终代码如下:

 class Solution
{
public:
bool isBalanced(TreeNode *root)
{
if(!root)
{
return true;
} isBalanced_ = true; GetDepth(root); return isBalanced_;
} private:
int GetDepth(TreeNode* node)
{
//已经不平衡后不需要再继续了,返回的深度也没用了,随便返回一个0即可
if(!isBalanced_)
{
return ;
} if(!node->left && !node->right)
{
return ;
} //当前节点贡献1的深度
int left = ;
int right = ; if(node->left)
{
left += GetDepth(node->left);
} if(node->right)
{
right += GetDepth(node->right);
} if(left > right + || right > left + )
{
isBalanced_ = false; return ;
} return left > right ? left : right;
} bool isBalanced_; };

isBalanced

[LeetCode 110] - 平衡二叉树 (Balanced Binary Tree)的更多相关文章

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

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

  2. 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树

    平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...

  3. LeetCode OJ:Balanced Binary Tree(平衡二叉树)

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

  4. LeetCode(110) Balanced Binary Tree

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

  5. 【LeetCode OJ】Balanced Binary Tree

    Problem Link: http://oj.leetcode.com/problems/balanced-binary-tree/ We use a recursive auxilar funct ...

  6. [Swift]LeetCode110. 平衡二叉树 | Balanced Binary Tree

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

  7. 平衡二叉树Balanced Binary Tree

    [抄题]: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...

  8. LeetCode算法题-Balanced Binary Tree(Java实现)

    这是悦乐书的第167次更新,第169篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第26题(顺位题号是110).给定二叉树,判断它是否是高度平衡的.对于此问题,高度平衡二 ...

  9. LeetCode题解之Balanced Binary Tree

    1.题目描述 2.问题分析 DFS. 3.代码 bool isBalanced(TreeNode* root) { if (root == NULL) return true; && ...

随机推荐

  1. N - 畅通工程再续 - hdu 1875

    Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题 ...

  2. POJ3422 Kaka's Matrix Travels 【最大费用最大流】

    Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8006   Accepted:  ...

  3. [转] 查看CPU使用率 top命令详解

    一 top是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前 台,直到用户终止该程序为止. 比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示 ...

  4. json 项目应用

    package com.founder.ec.dec.action; import java.net.URLEncoder; import java.util.HashMap; import java ...

  5. 黑信 socket即时通讯 示例

    整个过程 首先开启服务器 打开一个SDK大于4.4的手机---B 打开一个SDK小于4.4的手机---A 相互发送一条消息,对方就可以收到,当然这些消息都是通过服务器[转发]过来的 MainActiv ...

  6. String对象之间的比较

    public class StringTest { @Test public void test01() { int a = 50; // 基本数据类型比较的是值 int b = 50; System ...

  7. HTML5摇一摇

    方式一 (function(){ /** * 摇一摇 * @author rubekid */ function Shake(options){ this.init(options); } Shake ...

  8. python面对对象编程------3:写集合类的三种方法

    写一个集合类的三种方法:wrap,extend,invent 一:包装一个集合类 class Deck: def __init__( self ): self._cards = [card6(r+1, ...

  9. java transient关键字和transaction的区别

    transient:表示临时的,不会被持久化,保存进数据库 transaction:表示事务 <div style="background: #fff; color: #0ff;&qu ...

  10. (转)javascript中return的作用

    这里面的return含有一些细节知识: 例如:onClick='return add_onclick()'与 onClick='add_onclick()'的区别 JAVASCRIPT在事件中调用函数 ...