Skip to content

Instantly share code, notes, and snippets.

@sanketfirodiya
Created May 12, 2015 03:16
Show Gist options
  • Save sanketfirodiya/eabdc41003545f84ca75 to your computer and use it in GitHub Desktop.
Save sanketfirodiya/eabdc41003545f84ca75 to your computer and use it in GitHub Desktop.
Check if BST is valid
+ (BOOL)isBSTValid {
return [self validate:self.root withMin:MIN.INTEGER withMax:MAX.INTEGER];
}
- (BOOL)validate:(Node *)root withMin:(NSUInteger)min withMax:(NSUInteger)max {
if (root.value <= min || root.value >= max) {
return NO;
}
return ([self validate:root.left withMin:min withMax:root.Value] && [self validate:root.right withMin:root.value withMax:max]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment