Trees: Is This a Binary Search Tree? HackerRank Solution


Trees: Is This a Binary Search Tree? HackerRank Solution
Source : https://www.hackerrank.com/challenges/ctci-is-binary-search-tree



Source : https://www.hackerrank.com/challenges/ctci-is-binary-search-tree


Solution


// Karthikalapati.blogspot.com
/*
The Node class is defined as follows:
class Node {
int data;
Node left;
Node right;
}
*/
boolean checkBST(Node root) {
return checkBST(root, 0, 10000); // range of values in problem
}
boolean checkBST(Node node, int min, int max) {
if (node == null) {
return true;
} else if (node.data < min || node.data > max) {
return false;
}
return checkBST(node.left, min, node.data - 1) && checkBST(node.right, node.data + 1, max);
}

No comments:

Post a Comment