题目

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. ZROI 部分题目题解

    ZROI 部分题目题解 335 首先发现一个性质: 对于最短的边而言,所有点的路径如果经过了这条边,那么路径的权值就是这条边的边权(废话) 那么我们把最短的边拎出来,可以发现,博物馆确定时,每个点按照 ...

  2. AKOJ-1695-找素数

    题意: 给定区间L,R. 计算区间中素数个数. 2 <= L,R <= 2147483647, R-L <= 1000000. 思路: 素数区间筛 先筛(2-sqrt(r)). 再用 ...

  3. chrome浏览器历史版本

    持续更新中······ google-chrome 浏览器 win64位 版本号 大小 官网更新日期 66.0.3359.181 48.58 MB 2018年5月16日 65.0.3325.181 4 ...

  4. RTOS之CMSIS-RTOS

    CMSIS-RTOS 是实时操作系统的通用 API.它提供了标准化的编程接口,它只是封装了RTX/embos,以后还可能封装freeRTOS,uc/os等等第三方OS,CMSIS RTOS是ARM现在 ...

  5. Python相对导入导致SystemError的解决方案(译)

    原文出处: http://stackoverflow.com/   译文出处:yibohu1899 这个问题是如何解决在相对导入的时候,如果出现’System Error’的时候的解决方案.顺带一提, ...

  6. sql server 日期 查询技巧

    CONVERT(varchar(100), SendTime, 23) –-sql里的字段SendTime参数 selectdateName(weekday,getDate());--返回当前星期 s ...

  7. vs2013编译过程中,错误 59 error C4996: 'GetVersionExW': 被声明为已否决

    好几次碰到这个错误,必须mark 一下!!!!!Project Properties > Configuration Properties > C/C++ > General > ...

  8. LINUX一网卡多IP设置

    方法1:少量IP手动绑定(这里以绑定IP到eth0为例,其它网卡的话修改相应的文件名即可) 1.复制ifcfg-eth0的网卡配置文件并改名为ifcfg-eth0:0 [root@akinlau /] ...

  9. ARC和MRC混合模式下的编译问题

    在一个支持ARC (Automatic Reference Counting)的项目中,有时候需要禁止其中几个文件使用ARC模式编译(比如你用了第三方不支持ARC的类库).这时就要点击工程文件,在ta ...

  10. iOS 查看包架构信息

    lipo -info libUMSocial_Sdk_4.2.a 查看包架构信息