Java [Leetcode 110]Balanced Binary Tree
2016-01-12 17:50
393 查看
题目描述:
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.
解题思路:
递归法解题。
代码如下:
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.
解题思路:
递归法解题。
代码如下:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isBalanced(TreeNode root) { if(root == null) return true; else{ if(Math.abs(getHeight(root.left) - getHeight(root.right)) > 1) return false; else return isBalanced(root.left) && isBalanced(root.right); } } public int getHeight(TreeNode root){ if(root == null) return 0; else return 1 + Math.max(getHeight(root.left), getHeight(root.right)); } }
相关文章推荐
- 详解Spring MVC 4常用的那些注解
- 查看java类是从哪个包加载
- 在spring MVC的controller中获取ServletConfig
- SSL介绍与Java实例
- Selenium+java环境搭建
- Android with Eclipse - Waiting for HOME ('android.process.acore') to be launched?
- java图片加水印文字
- java中遍历Map集合的集中方法总结
- java 多线程(1) join() / interrupt() 打断sleep() / stop() / run() / 线程停止的方法
- Java基础学习笔记5
- java.lang.OutOfMemoryError
- 关于java Udp收发数据异常的笔记
- spring.xml等xml文件的注解解读
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- nested exception is java.lang.NoSuchFieldError: TRACE
- Java Mail(三):JavaMail发送文本邮件换行
- java中Keytool的使用总结
- java之生成不重复的随机数
- springMVC注解驱动
- 【javaMail】【3.多线程发邮件】java mail 多线程发送邮件