Last active
April 26, 2016 06:51
-
-
Save sanketfirodiya/3b7b9dfbbeb8bc97a30b to your computer and use it in GitHub Desktop.
Check if BST is balanced
This file contains hidden or 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
- (BOOL)isBSTBalanced:(Node *)root { | |
return ([self maxHeight:root] - [self minHeight:root] <= 1); | |
} | |
- (NSUInteger)maxHeight:(Node *)root { | |
if (root == null) { | |
return 0; | |
} | |
return MAX([self maxHeight:root.leftNode], [self maxHeight:root.rightNode]) + 1; | |
} | |
- (NSUInteger)minHeight:(Node *)root { | |
if (root == null) { | |
return 0; | |
} | |
return MIN([self minHeight:root.leftNode], [self minHeight:root.rightNode]) + 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment