110. Balanced Binary Tree
2016-03-12 14:29
471 查看
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.
Subscribe to see which companies asked this question
Hide Tags
Tree Depth-first Search
Hide Similar Problems
(E) Maximum Depth of 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.
Subscribe to see which companies asked this question
Hide Tags
Tree Depth-first Search
Hide Similar Problems
(E) Maximum Depth of Binary Tree
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isBalanced(TreeNode root) { if (depth(root) == -1) return false; return true; } private int depth(TreeNode root) { if (root == null) return 0; int left = depth(root.left); int right = depth(root.right); // If any subtree fails, all parent nodes should fail. No need to calculate depth. if (left == -1 || right == -1 || Math.abs(left-right) > 1) return -1; return Math.max(left,right)+1; } }
相关文章推荐
- 数据库中索引的结构和什么情况下不适合建索引,数据库索引是如何实现的?
- lua-数组
- RPC, SOAP, WSDL, REST
- Xmlns:android
- 5. 用 PHP 源码操作 memcache(2)
- 剑指offer-不用加减乘除做加法
- Ubuntu 14.04安装Caffe
- shell 横杆 转义
- Item 25: 对右值引用使用std::move,对universal引用则使用std::forward
- 获取SHA1值
- Unity_2D游戏实例从零讲起(3)——基本菜单UI的实现
- 部署网站常用
- 树的同构
- unity视频笔记——ngui-基础
- 基因数据处理6之BWA_MEM无法分配内存
- 解决Linux下inode频繁掉线问题
- 关于Web API的个人理解
- python-Day7
- jquery.validate.unobtrusive的使用
- 堆栈之静态数组