LeetCode 110 Balanced Binary Tree
2016-04-06 23:45
357 查看
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) { return depth(root) >= 0; } public int depth(TreeNode root) { if (root == null) return 0; int highL = depth(root.left); int highR = depth(root.right); if (Math.abs(highL - highR) > 1 || highL < 0 || highR < 0) return -1; return Math.max(highL, highR) + 1; }
相关文章推荐
- Java开发工具
- CF 620 C. Pearls in a Row 贪心
- n x n矩阵的转置矩阵,并计算对角线元素之和
- 微软201604笔试题目3 Demo Day 动态规划解法
- angularjs 前端页面之用户修改密码
- Android后台定时提醒功能实现
- 匿名内部类
- 2016年4月六日总结
- 总结二、MFC消息映射
- onvif学习笔记6:onvif的OSD坐标小记
- SDAU 搜索专题 15 Knight Moves
- [IOS]tableView的操作。
- 232. Implement Queue using Stacks
- LeetCode *** 235. Lowest Common Ancestor of a Binary Search Tree
- 八、九章知识点整理
- IRC使用
- STL与泛型编程<十七>:STL算法简介及for_each()算法
- 文章标题
- php类库
- 多线程通信