Skip to content

Instantly share code, notes, and snippets.

@sanketfirodiya
Last active April 26, 2016 06:51
Show Gist options
  • Save sanketfirodiya/3b7b9dfbbeb8bc97a30b to your computer and use it in GitHub Desktop.
Save sanketfirodiya/3b7b9dfbbeb8bc97a30b to your computer and use it in GitHub Desktop.
Check if BST is balanced
- (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