Created
July 13, 2014 17:31
-
-
Save iwilbert/bb5372cf358ff1981548 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean isBalanced(Tree root) { | |
if (checkHeight(root) == -1) | |
return false; | |
else | |
return true; | |
} | |
private int checkHeight(Tree root) { | |
if (root == null) | |
return 0; | |
int left_h = checkHeight(root.left); | |
if (left_h == -1) | |
return -1; | |
int right_h = checkHeight(root.right); | |
if (right_h == -1 || Math.abs(left_h - right_h) > 1) | |
return -1; | |
return Math.max(left_h, right_h) + 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment