Leetcode 110. Balanced Binary Tree
2016-03-28 21:42
441 查看
Question
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
4000
than 1.
Code
public int get(TreeNode root) { if (root == null) { return 0; } int ldep = get(root.left); if (ldep == -1) { return -1; } int rdep = get(root.right); if (rdep == -1) { return -1; } if (Math.abs(ldep - rdep) >= 2) { return -1; } return Math.max(rdep + 1, ldep + 1); } public boolean isBalanced(TreeNode root) { int result = get(root); return result != -1; }
相关文章推荐
- 求两个数的最大公约数
- 总结
- HDU 4595 Similar Number (主席树)
- Remember the Word,LA3942(Trie树+DP)
- 整数求和
- kmp算法字符串匹配C语言实现
- AndroidStudio R.java文件丢失的问题
- 光电传感器笔记
- 单链表-----数据结构
- JavaScript原型链
- HanLP里使用DAT存取字典的方法
- iOS 中socket的总结
- POJ 2376 Cleaning Shifts 区间贪心
- Remove Node in Binary Search Tree
- 屏幕参数
- Linux系统中校验下载文件的完整性方法(MD5,SHA1,PGP)
- 求两个数的最大公约数
- 设计模式:单例模式
- 归并排序求逆序对数目
- RealtimeBoard(可视化协同工作白板)