Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
/* check for BST */
private static boolean isBST(BSTNode root) {
if (root == null)
return true;
if (root.getLeft() != null && findMax(root.getLeft()) > root.getData())
return false;
if (root.getRight() != null
&& findMin(root.getRight()) < root.getData())
return false;
if (!(isBST(root.getLeft())) || (!isBST(root.getRight())))
return false;
return true;
}
It is simple, you can do an in-order traversal of the tree and every time you read the root, put that to an array, then you can go through the array by comparing elements which should be in ascending order.
public void checkBST(Node tree){
if(tree!=null){
checkBST(tree.left);
arrayList.put(tree.data);
checkBST(tree.right);
}
}
static boolean isBST(TreeNode root){
if(root == null)
return true;
if(root.left!=null && root.left.data > root.data ||
root.right!=null && root.right.data < root.data)
return false;
return(isBST(root.left) && isBST(root.right));
}
Thanks....
Here is the new one
static boolean isBST(TreeNode root){
return isBST(root,Integer.MIN_VALUE,Integer.MAX_VALUE);
}
static boolean isBST(TreeNode root,int min,int max){
if(root == null)
return true;
if(root.data<min || root.data>max)
return false;
if(root.left!=null && root.left.data > root.data ||
root.right!=null && root.right.data < root.data)
return false;
return(isBST(root.left) && isBST(root.right));
}
If you have a tree how to verify it a BST
static boolean isBST(Node root) {
if(root == null)
return false;
if(root.left != null && root.left.value < root.value) {
return true;
}
if(root.right != null && root.right.value > root.value) {
return true;
}
if((root.left != null && root.left.value > root.value)
|| (root.right != null && root.right.value > root.value)) {
return false;
}
isBST(root.left);
isBST(root.right);
return true;
}
better Solution will be
static boolean isBST(TreeNode root){
if(root == null)
return true;
if(root.left!=null && root.left.data > root.data ||
root.right!=null && root.right.data < root.data)
return false;
return(isBST(root.left) && isBST(root.right));
}
static boolean isBST(TreeNode root){
return isBST(root,Integer.MIN_VALUE,Integer.MAX_VALUE);
}
static boolean isBST(TreeNode root,int min,int max){
if(root == null)
return true;
if(root.data<min || root.data>max)
return false;
if(root.left!=null && root.left.data > root.data ||
root.right!=null && root.right.data < root.data)
return false;
return(isBST(root.left,min,root.data) && isBST(root.right,root.data,max));
}
can you please tell me your algo.i don't think anyone doing here is correct for checking BST except inorder traversal and storing in array and then check it is sorted or not.
pls explain me your algo .
thanks
I'm not sure why everybody wanna make it so complex :)
The question was simple and my answer was straight forward and the Amazon interviewer agreed with me when I mentioned the inorder traversal solution. I guess there may be different solutions, but don't make it over complicated, you have only may be 10 mintues to discuss the algo and then code it correctly.
int isThisABST(struct node* mynode)
{
if (mynode==NULL) return(true);
if (node->left!=NULL && maxValue(mynode->left) > mynode->data) return(false);
if (node->right!=NULL && minValue(mynode->right) <= mynode->data) return(false);
if (!isThisABST(node->left) || !isThisABST(node->right)) return(false);
return(true);
}
Reference nice material
- Vir Pratap Uttam May 05, 2015