LeetCode:Balanced Binary Tree
2015-10-23 13:28
323 查看
标题叙述性说明:
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.
思路:当树的左子树的高度与右子树的高度相差小于一,且左右子树各自都为平衡二叉树时,树为平衡二叉树。
代码:
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.
思路:当树的左子树的高度与右子树的高度相差小于一,且左右子树各自都为平衡二叉树时,树为平衡二叉树。
代码:
bool Solution::isBalanced(TreeNode *root) { if(root == NULL) return true; if(abs(treeHeight(root->left) - treeHeight(root->right)) > 1) return false; return isBalanced(root->left) && isBalanced(root->right); } int max(int a,int b) { return a > b ? a : b; } int Solution::treeHeight(TreeNode * root) { if(root == NULL) return 0; return max(treeHeight(root->left),treeHeight(root->right)) + 1; }
相关文章推荐
- 求解决一个单链表插入排序的问题
- win7运行bat文件 一闪而过 解决
- Hadoop集群搭建的密钥配置SSH实现机制的配置(2)
- 更改Eclipse下Tomcat的部署目录 ,防止上传的文件是到eclipse的克隆的tomcat上的webapp,而不是tomcat本身的webapp
- 搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)
- IOS网络笔记--音乐播放器
- Boost源码学习二[内存管理一]中
- SQL中的cast()函数介绍
- spring @Resourse注解使用方法,建议使用Resourse
- 对进程和线程一个形象的比喻
- PHP设计模式三:原型设计模式
- linux mint上无法登陆mate桌面
- nodejs实战express笔记之增加编辑与删除功能
- C++ 类的静态成员详细讲解
- 参数估计基础(一)
- RDF和Jena RDF API简介
- 程序员生意经
- vs中建立动态链接库的步骤
- Java中的异常复习
- NYOJ 108 士兵杀敌(一)(线段树—区间求和)