如果左子树是AVL树,右子树是AVL树,他们的高度差小于等于1,那就是AVL树;否则不是.
public class IsAVLTree {
public static boolean isAVL(TreeNode root) {
if (root==null) {
return true;
}
int diff=Math.abs(getHeigt(root.lchild)-getHeigt(root.rchild));
if (isAVL(root.lchild)&&isAVL(root.rchild)&&diff<=1) {
return true;
} else {
return false;
}
}
public static int getHeigt(TreeNode root) {
if (root==null) {
return 0;
}
return Math.max(getHeigt(root.lchild), getHeigt(root.rchild))+1;
}
}
class TreeNode{
int data;
TreeNode lchild;
TreeNode rchild;
}