Balanced Binary Tree
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.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
//两个递归,一个递归求树的高度,另一个递归求左右树是否平衡
if (root == NULL)
return true;
int l= height(root->left);
int r= height(root->right);
if(l-r>=-1&&l-r<=1)
return isBalanced(root->left)&&isBalanced(root->right);
else
return false;
}
int height (TreeNode* root)
{
if(root==NULL)
return 0;
else
{if (root->left ==NULL )
return height(root->right)+1;
if(root->right == NULL)
return height(root->left)+1;
else
{
int l= height(root->left)+1;
int r= height(root->right)+1;
return max(l,r);
}
}
}
};
- css中filter:alpha透明度使用小结兼容IE、火狐
- Android之警告对话框alertdialog
- svn 上传.a文件失败问题
- android转eclipse
- 查看图片列表,选中之后查看大图,可缩放滑动
- Android studio aidl文件的创建使用
- Android 程序中禁止屏幕旋转和重启Activity
- 组合模式的实际用处-文件系统
- 查询单表菜单是,sql
- java Exception
- QML使用Sqlite数据库
- Android网络视频播放器DEMO
- jsp中怎么判断密码中不能含有空格,帐号只能由字母、数字、汉字、下划线“_”组成
- 一句话自我介绍
- 转载:do{}while(0)
- 怎样设计一个运行日志服务
- 不通过App Store,在iOS设备上直接安装应用程序
- 日常收集整理的JavaScript常用函数方法
- JavaEE_Mybatis_SpringMVC SpringMVC日期类型转换问题三大处理方法归纳
- 华为OJ中级题-密码验证合格程序