题目

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

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

分析

判断给定二叉树是否为二叉平衡树,即任一节点的左右子树高度差必须小于等于1,只需要求得左右子树的高度,比较判定即可。

AC代码

class Solution {
public:
//判断二叉树是否平衡
bool isBalanced(TreeNode* root) {
if (!root)
return true; int lheight = height(root->left), rheight = height(root->right); if (abs(lheight - rheight) <= 1)
return isBalanced(root->left) && isBalanced(root->right);
else
return false;
}
//求树的高度
int height(TreeNode *root)
{
if (!root)
return 0;
else
return max(height(root->left), height(root->right)) + 1;
}
};

GitHub测试程序源码

LeetCode(110) Balanced Binary Tree的更多相关文章

  1. LeetCode(24)-Balanced Binary Tree

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

  2. LeetCode(114) Flatten Binary Tree to Linked List

    题目 分析 按要求转换二叉树: 分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树). 发现规则,便容易处理了.得到先序遍历,构造即可. AC代码 /** * De ...

  3. LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal

    题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...

  4. LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal

    题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...

  5. LeetCode(226)Invert Binary Tree

    题目 分析 交换二叉树的左右子树. 递归非递归两种方法实现. AC代码 class Solution { public: //递归实现 TreeNode* invertTree(TreeNode* r ...

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

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

  7. leetcode笔记(二)94. Binary Tree Inorder Traversal

    题目描述 (原题目链接) Given a binary tree, return the inorder traversal of its nodes' values. For example:Giv ...

  8. LeetCode(99) Recover Binary Search Tree

    题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...

  9. LeetCode(98) Validate Binary Search Tree

    题目 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined ...

随机推荐

  1. HDU6308(2018多校第一场)

    Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6308 将时间化简为分钟计算,同时不要用浮点数计算,精度会出现问题: 如果采用精度,最好加 ...

  2. c/c++学习系列之putchar、getchar、puts、gets的运用

    如果您只想取得使用者输入的字元,則可以使用getchar(),它直接取得使用者輸入的字元并传回,如果只想要输出一個字元,則也可以直接使用putchar(),以下是个简单的例子: #include &l ...

  3. Spring的ioc(DI)复习概念和原理简介

    IOC的好处 ioc或者说di的概念很显然了,反转控制和依赖注入,那本来直接new就行的东西,为什么要搞这么复杂呢?? 开发维护方便,高层设计不用依赖底层的,不然底层一个类改下构造器,高层就全要改,因 ...

  4. 对象(Object)和类(Class)的关系?

    对象属于某一类,即对象是某一个类的实例.例如: Public Class Flight Private _name As String Public Property Name As String G ...

  5. log4j2 日志框架小记

    这两天开始学习日志框架了, 把常用的学习一下,记录一下.上篇日志写了log4j-----https://www.cnblogs.com/qiaoyutao/p/10995895.html今天就总结一下 ...

  6. js图片预加载以及延迟加载

    当我们需要做图片轮播的时候,如果让图片提前下载到本地,用浏览器缓存起来,我们可以用Image对象: function preLoadImg(){ var img=new Image(); img.sr ...

  7. node.js0-5初级者

    伴着<妈是心中的茉莉花> 这里,我用的sublime记事本,所以用的运行方法是终端.(后来发现git 可以省去cd切换目录). 安装node.js  官网说的很清楚. 这里我们可以在js文 ...

  8. Objective-C 类型转换

    类型转换通常是指变量,从一种类型转换成另外一种类型.例如将一个long类型转换成int类型,变量转换通常 用下面的方式: (type_name) expression 在Objective-C中,我们 ...

  9. 面试题--JAVA中静态块、静态变量加载顺序

    最后给大家一道面试题练练手,要求写出其结果(笔试) public class StaticTest { public static int k = 0; public static StaticTes ...

  10. 洛谷 P2936 [USACO09JAN]全流Total Flow

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...