Skip to content

Instantly share code, notes, and snippets.

@iwilbert
Created July 13, 2014 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwilbert/bb5372cf358ff1981548 to your computer and use it in GitHub Desktop.
Save iwilbert/bb5372cf358ff1981548 to your computer and use it in GitHub Desktop.
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