Skip to content

Commit 11a5e42

Browse files
committed
Updated to check if left is less and right is more than root.
1 parent abff04b commit 11a5e42

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/chapter06trees/CheckAVL.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
package chapter06trees;
1414

1515
public class CheckAVL {
16-
public boolean isAVL(BinarySearchTreeNode root) {
16+
public boolean isAVL(BinarySearchTreeNode root, int min, int max) {
1717
if(root == null)
1818
return true;
19-
return isAVL(root.left) && isAVL(root.right) && Math.abs(getHeight(root.left) - getHeight(root.right)) <= 1;
19+
return root.data > min && root.data < max && isAVL(root.left, min, root.data) && isAVL(root.right, root.data, max) && Math.abs(getHeight(root.left) - getHeight(root.right)) <= 1;
2020
}
2121
public int getHeight(BinarySearchTreeNode root){
2222
int leftHeight, rightHeight;
@@ -31,4 +31,8 @@ public int getHeight(BinarySearchTreeNode root){
3131
return rightHeight + 1;
3232
}
3333
}
34-
}
34+
35+
public boolean isAVL(BinarySearchTreeNode root){
36+
return isAVL(BinarySearchTreeNode root, Integer.MIN_VALUE, Integer.MAX_VALUE);
37+
}
38+
}

0 commit comments

Comments
 (0)