http://blog.csdn.net/qq_27703417/article/details/70960005
有一棵二叉树,请设计一个算法判断这棵二叉树是否为平衡二叉树。给定二叉树的根结点root,请返回一个bool值,代表这棵树是否为平衡二叉树。
- import java.util.*;
- public class CheckBalance {
- public boolean check(TreeNode root) {
-
-
- boolean result=this.getHeight(root)==-1?false:true;
- return result;
- }
-
- private int getHeight(TreeNode root){
-
- if(root==null) return 0;
-
- int leftHeight=this.getHeight(root.left);
-
- if(leftHeight==-1) return -1;
-
- int rightHeight=this.getHeight(root.right);
-
- if(rightHeight==-1) return -1;
-
- if(Math.abs(leftHeight-rightHeight)>1) return -1;
-
- return Math.max(leftHeight,rightHeight)+1;
- }
- }
平衡二叉树判断
原文:http://www.cnblogs.com/joshsung/p/7407326.html