题目

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代码

  1. class Solution {
  2. public:
  3. //判断二叉树是否平衡
  4. bool isBalanced(TreeNode* root) {
  5. if (!root)
  6. return true;
  7. int lheight = height(root->left), rheight = height(root->right);
  8. if (abs(lheight - rheight) <= 1)
  9. return isBalanced(root->left) && isBalanced(root->right);
  10. else
  11. return false;
  12. }
  13. //求树的高度
  14. int height(TreeNode *root)
  15. {
  16. if (!root)
  17. return 0;
  18. else
  19. return max(height(root->left), height(root->right)) + 1;
  20. }
  21. };

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. bryce1010专题训练——Splay树

    Prob Hint BZOJ 3323 文艺平衡树 区间翻转 BZOJ 1251 序列终结者 区间翻转,询问最值 BZOJ 1895 supermemo 区间加,翻转,剪切,询问最值.点插入,删除. ...

  2. CATIA 基础详解 第01章 CATIA初认识

    1.1 CATIA V5产品介绍 CATIA V5是基于美国IBM公司与法国达索系统公司(Dassault Systèmes)软件解决方案推出的新一代产品,它致力于满足以设计流程为中心的设计需求.它提 ...

  3. oracle把一个表的数据复制到另一个表中

    http://blog.csdn.net/my_name_nb/article/details/64128015 ........................ 1. 新增一个表,通过另一个表的结构 ...

  4. 06.Javascript——入门this的用法(难点)

    this 的指向 this 是 js 中定义的关键字,它自动定义于每一个函数域内,但是它的指向却让人很迷惑.在实际应用中,this 的指向大致可以分为以下四种情况. 1.作为普通函数调用 当函数作为一 ...

  5. corn表达式 经典

    https://www.cnblogs.com/GarfieldTom/p/3746290.html

  6. winform重绘

    1.重绘文字#多行文字a.先定义一个矩形 Rectangle p1 = , , , this.Height); Rectangle p2 = , , , this.Height); Rectangle ...

  7. Linux的安装与配置

    PS:本文适合刚刚了解Linux系统,并想要学习Linux系统的一些基本操作的同学.只要按如下方法安装配置好,就可以在自己的电脑上使用Linux系统了. 一.安装前的准备 1.下载并安装VMware ...

  8. 状态压缩---状态压缩dp第一题

    标签: ACM 题目: Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...

  9. ios invalid put policy encoding 七牛上传报错

    获取七牛token的时候deadline不能为NSString类型 NSDictionary *infoDic = @{@"scope":@"yangtao", ...

  10. fluent_python1

    Magic Method python中有些跟对象本身有关的方法, 以两个下划线开始,两个下划线结束, 一般称为魔法方法(magic method). 比如 obj[key] 的背后就是 __geti ...