递归

  • 左子树是否为平衡二叉树
  • 右子树是否为平衡二叉树
  • 左右子树高度差是否大于1,大于1,返回false
  • 否则判断左右子树

最简单的理解方法就是如下的思路:

class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root==NULL){
return true;
}
int ldepth=depth(root->left);
int rdepth=depth(root->right);
if(abs(ldepth-rdepth)>)return false;
else{
return isBalanced(root->left)&&isBalanced(root->right);
}
}
int depth(TreeNode *root){
if(root==NULL){
return ;
}
int lleft=depth(root->left);
int trigth=depth(root->right);
return lleft>trigth ? lleft+:trigth+; }
};

在上面的方法中,每一个节点都会计算节点左子树和右子树的高度,存在大量的重复计算

所以,采用自底向上的方式

class Solution2 {
public:
bool isBalanced(TreeNode* root) {
if(root==NULL){
return true;
}
int height=getheight(root);
if(height==-){
return false;
}
return true;
}
int getheight(TreeNode *root){
if(root==NULL){
return ;
}
int lleft=getheight(root->left);
int trigth=getheight(root->right);
if(lleft==-||trigth==-){
return -;
}
if(abs(lleft-trigth)>){
return -;
}
return lleft>trigth? lleft+:trigth+;
}
};

Balanced Binary Tree --Leetcode C++的更多相关文章

  1. 110.Balanced Binary Tree Leetcode解题笔记

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

  2. 110. Balanced Binary Tree - LeetCode

    Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...

  3. Balanced Binary Tree [LeetCode]

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

  4. Balanced Binary Tree——LeetCode

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

  5. Balanced Binary Tree leetcode java

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

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  8. LeetCode: Balanced Binary Tree 解题报告

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

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

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

随机推荐

  1. T4模板之基础篇

    一.回顾 上一篇文章 ——T4模板之菜菜鸟篇,我们囫囵吞枣的创建了与“T4模板”有关的文件.在创建各个文件的这一个过程中,我们对于T4模板有了那么丁点的认识.现在就带着之前的那些问题,正式的迈入对“T ...

  2. OO真经——关于面向对象的哲学体系及科学体系的探讨(下)

    真经第六章——运作 Moving “运动是绝对的——牛顿” 6.1.导言 在前五章中,我们从世界观的这话题开始,逐步引出了抽象.层次.继承和耦合.这些内容,形成了对象论中关于世界的结构体系.      ...

  3. webpack和webpack-dev-server的区别

    第一: webpack只是构建 webpack-dev-server除了构建,还提供web服务   第二:webpack.config.json的路径参数 显然,entry都一样,因为都要知道需要构建 ...

  4. Hadoop基础概念介绍

    基于YARN的配置信息, 参见: http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ hadoop入门 - 基础概念 ...

  5. windows系统中的dll的作用详细解释

    什么是.DLL文件? DLL 是一个包含可由多个程序同时使用的代码和数据的库.例如,在 Windows 操作系统中,Comdlg32 DLL 执行与对话框有关的常见函数.因此,每个程序都可以使用该 D ...

  6. poj 3270 置换

    poj 置换的应用 黑书原题P248 /** 题意: 给定序列, 将其按升序排列, 每次交换的代价是两个数之和, 问代价最小是多少 思路:1.对于同一个循环节之内的,肯定是最小的与别的交换代价最小 2 ...

  7. NET Core开发-使用Nancy框架

    NET Core开发-使用Nancy框架 Nancy简介 Nancy是一个轻量级的独立的框架,下面是官网的一些介绍: Nancy 是一个轻量级用于构建基于 HTTP 的 Web 服务,基于 .NET ...

  8. python进阶3--文件系统

    文件系统 python的标准库中包括大量工具,可以处理文件系统中的文件,构造和解析文件名,也可以检查文件内容. pyhton表文件名表示为简单的字符串,另外还提供了一些工具,用来由os.path中平台 ...

  9. Effective C++学习笔记——构造/析构/拷贝运算

    条款9:决不再构造和析构过程中调用virtual函数,包括通过函数间接调用virtual函数. 应用:想在一个继承体系中,一个derived class被创建时,某个调用(例如生成相应的日志log)会 ...

  10. Android 五大布局(LinearLayout、FrameLayout、AbsoulteLayout、RelativeLayout、TableLayout )

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...