LeetCode(110) Balanced Binary Tree
2015-10-14 15:02
411 查看
题目
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测试程序源码
相关文章推荐
- 聊聊灰度发布
- 【Nginx】显示器port管理
- 获取storyboard中的viewcontroller
- LeetCode(110) Balanced Binary Tree
- MAC PHP MARK
- 移动前端对树状结构的数据同步问题
- android color.xml --各种颜色的值
- ProE/Creo免费插件 MCADEx Tools 5.0
- 浏览器内核分类
- Java GC 算法
- Oracle 11g ORA-28002:口令将过期的解决方法
- seajs实例
- maven安装,maven配置,maven在eclipse中的配置(需要插件)
- 模板
- 微信公众平台开发关于“该公众号暂时无法提供服务,请稍后再试”解决方案
- HDU2027 统计元音
- HDU2027 统计元音
- map的查找
- Win7系统上配置使用Intellij Idea 13的SVN插件
- JS的splice()方法和slice()方法