110. Balanced Binary Tree
2016-02-03 17:38
267 查看
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.
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.
public boolean isBalanced(TreeNode root) { if(root == null) return true; return depth(root) == -1 ? false:true; } private int depth(TreeNode root) { //返回-1代表不平衡;返回正整数代表树的最大高度 if(root == null) return 0; int left = depth(root.left); if(left == -1) return -1; int right = depth(root.right); if(right == -1) return -1; if(right - left > 1 || right -left < -1) return -1; //左右子树不平衡 return right >= left ? right+1:left+1; }
相关文章推荐
- Js判断浏览器是否是移动设备
- Swift: 是用Custom Segue还是用Transition动画
- iOS:交换Button中图片与文字的左右位置
- Android Storage Manager
- Swift: 是用Custom Segue还是用Transition动画
- HDU1272 并查集
- SSH-Strut
- 解决在Windows Server 2008 R2安装Sitescope 10.10报错失败
- HDU 1394 Minimum Inversion Number (线段树 单点更新 求逆序数)
- C++学习笔记1
- maven使用命令行新建项目
- Permission denied (publickey)
- android 实现倒计时和计时的5种方法
- python 笔记 加密模块
- python 笔记 加密模块
- 排序七之堆排序
- Android酷炫实用的开源框架(UI框架)
- ssh-key Permission denied (publickey)
- 从Spark Streaming 收集应用程序日志到Flume agent的配置方法
- js 关闭窗口时判断表单数据是否改变