LeetCode:110. Balanced Binary Tree
2015-12-30 10:36
274 查看
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.
1.思路:计算深度 左右子数平衡 并且判断深度差值的绝对值小于等于1 return true!
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.
1.思路:计算深度 左右子数平衡 并且判断深度差值的绝对值小于等于1 return true!
<span style="font-size:18px;">public int depth(TreeNode root) { if (root == null) { return 0; } else{ int a = depth(root.left); int b = depth(root.right); return a>b ? a+1:b+1; } } public boolean isBalanced(TreeNode root) { if(root == null) { return true; } if(root.left == null && root.right == null) {return true;} int leftLength = depth(root.left); int rightLength = depth(root.right); if(Math.abs(leftLength - rightLength) > 1) { return false; }else { return isBalanced(root.left) && isBalanced(root.right); } }</span>
相关文章推荐
- Java实现截图并保存到本地
- 《从零开始学Swift》学习笔记(Day54)——抛出错误
- JDK自带的监控分析工具JConsole
- (一)s3c2440 地址分配讲解 (很难很纠结)
- 2015/12/30日-月总结(心很累,然而并没有暖用)
- Flume thrift source C++ Demo
- JavaScript onkeyup事件实现仿验证码判断
- 解决MySQL Server Instance Configuration Wizard卡住未响应的问题
- 《从零开始学Swift》学习笔记(Day54)——抛出错误
- leetcode之Sum Root to Leaf Numbers
- 部分银行卡号规则(二)
- 第十一周 层次遍历算法的验证
- Spring MVC 的搭建
- 数据结构课程设计之图书管理系统
- Dancing links
- 数组/List转换成字节缓冲区
- 过滤器验证登录
- window安装PHP的redis扩展
- (一) ARM 内存SDRAM 讲解
- 用CSV导入数据到数据库