Skip to content

Instantly share code, notes, and snippets.

@hilda8519
Created July 9, 2014 21:40
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 hilda8519/24a6435951a05ac1b8de to your computer and use it in GitHub Desktop.
Save hilda8519/24a6435951a05ac1b8de to your computer and use it in GitHub Desktop.
public class isBalanced {
private TreeNode root;
provate class Node{
int data;
Node *left,*right;
}
public static int getHeight(TreeNode Root){
if(root==null){
return 0;
}
else{
return Math.max(getHeight(root.left),getHeight(root,right))+1;
}
public static boolean isBalanced(TreeNode root){
if(root==null){
return true;
}
int heightDifferent=getHeight(root.left)-getHeight(root.right);
if(Math.abs(heightDifferent)>1){
return false;
}
else{
return isBalanced(root.left)&&isBalanced(root.right);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment