问题

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

对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于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. SSLStrip 的未来 —— HTTPS 前端劫持

    前言 在之前介绍的流量劫持文章里,曾提到一种『HTTPS 向下降级』的方案 —— 将页面中的 HTTPS 超链接全都替换成 HTTP 版本,让用户始终以明文的形式进行通信. 看到这,也许大家都会想到一 ...

  2. java排序算法-归并排序

    public class MergeSort { private static void mergeSortTest() { int[] in = { 2, 5, 3, 8, 6, 7, 1, 4, ...

  3. jQuery.isEmptyObject()函数用于判断指定参数是否是一个空对象。

    jquery中有一个函数isEmptyObject()用来判断制定参数是否是一个空对象. 示例如下: function isEmptyObject(e) { var t; for (t in e) r ...

  4. SecureCRT连接vm中的ubuntu

    如何使用SecureCRT连接ubuntu 用secureCRT连接Ubuntu是出现远程系统拒绝访问..经过一翻研究才知道Ubuntu上没有ssh.. 一下为连接过程. 1. 首先要明白什么是ssh ...

  5. cocoa Touch-UIKit

    1. 简介: UIKitk框架提供一系列的Class(类)来建立和管理iPhone OS应用程序的用户界面( UI )接口.应用程序对象.事件控制.绘图模型.窗口.视图和用于控制触摸屏等的接口. (P ...

  6. 在JAVA中 System.getProperty 和 System.setProperty 方法.

    今天着手研究TOMCAT源码. 在刚開始的时候Startup类中init方法中调用非常多次System.getProperty和System.setProperty的方法. 后来经过网上搜索才得知,这 ...

  7. java.lang.UnsatisfiedLinkError: Native method not found 三种可能解决方案

    so文件编译生成后,运行时,有时候会遇到java.lang.UnsatisfiedLinkError: Native method not found问题,有可能是以下三种因素: 一.Jni方法头部大 ...

  8. [Javascript] What is JavaScript Function Currying?

    Currying is a core concept of functional programming and a useful tool for any developer's toolbelt. ...

  9. Swift学习笔记 - 函数与闭包

    import Foundation //1.函数的定义与调用//以 func 作为前缀,返回箭头 -> 表示函数的返回类型func sayHello(name: String) -> St ...

  10. Chapter 4. Using the Gradle Command-Line 使用gradle命令行

    This chapter introduces the basics of the Gradle command-line. You run a build using the gradle comm ...