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);
}

1 comment:


  1. 爱游戏体育app为亚洲最大持有合法牌照的投注平台、全球领先的娱乐服务供应商、华人第一品牌,我们有超过7,800,000平台会员。您一定从身边的朋友那里听说过爱游戏,朋友的口碑一定比文字更加能说服您。爱游戏大规模的赞助活动也是品牌实力的背书,曼联、拜仁慕尼黑、巴黎圣日尔曼、AC米兰、里昂、阿根廷国家队等等,这些品牌背后都有爱游戏的影子,带给您富有信心的安全保障。

    #爱游戏 #爱游戏体育 #爱游戏app #爱游戏官网

    ReplyDelete