Java实现LeetCode 110. Balanced Binary Tree
2024-10-09 04:50:48
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int height(TreeNode root){
if(root == null)
return 0;
int left_height = height(root.left);
int right_height = height(root.right);
return 1 + (left_height > right_height ? left_height : right_height);
}
public boolean isBalanced(TreeNode root) {
if(root == null){
return true;
}
int left_height = height(root.left);
int right_height = height(root.right);
if(Math.abs(left_height - right_height) > 1){
return false;
}
else{
return isBalanced(root.left) && isBalanced(root.right);
}
}
}
最新文章
- iOS Xcode behaviors个人常用Debug配置
- 【转】STM32定时器输出比较模式中的疑惑
- windows下的文件到linux下乱码 iconv 修改文件编码
- 以WCF安全认证方式调用通用权限管理系统获取基础信息资料
- django: template variable
- Linux内核和根文件系统引导加载程序
- [原]关于在 iOS 中支持 DLNA
- SSH整合总结(xml与注解)
- shiro入门示例
- Mysql 范围查询优化
- 携程Apollo(阿波罗)配置中心用户管理和部门管理
- 使用mongoskin操作MongoDB
- 第5次作业 -- 基于Jmeter的 性能测试
- <;转>;ajax 同步异步问题
- mysql打开binlog
- DHacker 汉化
- 微信公众平台测试帐号的注册与使用(自己的服务器<;---->;微信后台<;---->;测式公众号)
- BOM及DOM及事件
- Chapter 3 Phenomenon——17
- angularjs的Controller as