Created
July 9, 2014 21:40
-
-
Save hilda8519/24a6435951a05ac1b8de 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 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