问题

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

对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于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. mysql binaryVInstall

    下载mysql 1.下载:在http://dev.mysql.com/downloads/mysql/官网上下载mysql-5.5.28-linux2.6-i686.tar.gz. 2.解压 -lin ...

  2. input type=file accept中可以限制的文件类型

    在上传文件的时候,需要限制指定的文件类型. <input type="file" accept="image/*" /> accept表示可以上传文 ...

  3. const和violate

    const修饰变量 C语言中,const修饰的变量是只读的,本质还是变量,修饰的变量也会在内存中占用空间(这不废话么)本质上const只对编译器有用,在运行时无用.const和类型顺序无要求,一般类型 ...

  4. JAVA设计模式(09):结构型-代理模式(Proxy)

    代理模式是经常使用的结构型设计模式之中的一个,当无法直接訪问某个对象或訪问某个对象存在困难时能够通过一个代理对象来间接訪问,为了保证client使用的透明性,所訪问的真实对象与代理对象须要实现同样的接 ...

  5. codeforces 148D之概率DP

    http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test 2 seconds memory l ...

  6. ios开发应用内实现多语言自由切换

    需求描述:应用内部有一按钮,点击切换语言(如中英文切换).说起来这个是好久以前做的一个功能点了,刚开始也是没有头绪,后来解决了发现很简单,把方法分享一下.1.原理.查看NSLocalizedStrin ...

  7. PowerDesigner 生成带凝视SQL 各个版本号通用10(12、15)

    做数据库是设计时最苦恼的事就是用PowerDesigner工具设计完数据库运行SQL文件后没有凝视.那么怎么才干让PowerDesigner设计完有凝视呢,下边教你一个笨的方法,方法尽管笨,可是能实现 ...

  8. MFC中全局变量的定义及使用

    用MFC制作的工程由很多文件构成,它不能象一般C++程序那样随意在类外定义全局变量,在这里要想定义能被工程内多个文件共享的全局变量和函数必须用一些特殊方法才行.实际上有多种方法可以实现,这里只介绍两种 ...

  9. jquery.ajax提交多值(数组)

    偶尔会遇到类似复选框的一个属性存在多值情况,若使用ajax提交的化,设置data :{ids:[1,2,3,4]} 提交后,后台无法使用ids获取到数据. 这里可以用到ajax的 traditiona ...

  10. .net面试总结

    一. hr 为人处事 工作中遇到问题:沟通很重要 离职原因:公司倒闭 二. ISAPI Internet Server Application Program Interface 三. http状态码 ...