Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for BST related question #4

Closed
kapishmalik opened this issue Oct 2, 2018 · 2 comments
Closed

Solution for BST related question #4

kapishmalik opened this issue Oct 2, 2018 · 2 comments

Comments

@kapishmalik
Copy link

Your device hardware:

Issue details:
Given the root node of a binary tree, can you determine if it's also a binary search tree?
Complete the function which has parameter: a pointer to the root of a binary tree. It must return a boolean denoting whether or not the binary tree is a binary search tree. You may have to write one or more helper functions to complete this challenge.

boolean checkBST(Node root) {
return checkBSTUtil(root, null, null);
}
private boolean checkBSTUtil(Node root, Node min, Node max){
if(root == null){
return true;
}
if(min != null && min.data >= root.data){
return false;
}
if(max != null && root.data >= max.data){
return false;
}
return checkBSTUtil(root.left, min, root) && checkBSTUtil(root.right, root, max);
}

Log (optional):

@Kvaibhav01
Copy link
Owner

@kapishmalik kindly add this as a PR. Also, add the Hackerrank problem URL on your code just like I've done.

@kapishmalik
Copy link
Author

@Kvaibhav01 Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants