Define binary search tree. Develop a procedure to verify a binary search tree.
Réponses aux questions d'entretien
Utilisateur anonyme
22 févr. 2010
How do you verify a binary search tree? Will you get all the values from binary search tree and check if they are in order?
can someone explain?
1
Utilisateur anonyme
7 janv. 2013
bool IsValidBST(Node root, int min, int max)
{
if(root == null)
return true;
if (root.iData > min
&& root.iData < max
&& IsValidBST(root.LeftChild, min, root.iData)
&& IsValidBST(root.RightChild, root.iData, max))
return true;
else
return false;
}
1
Utilisateur anonyme
24 janv. 2010
Not very difficult. Asked for answer in code. Interviewer took notes for each line I wrote. Was interested in each statement I made about the algorithm, esp. why I took each approach.
1
Utilisateur anonyme
22 févr. 2010
@Bala
Recursively:
Check node.left node.value. Also, that all subvalues of node.left node.value. Make sure you do checking for edge cases (ie. leaf nodes, etc).
Utilisateur anonyme
22 févr. 2010
recursive solution is a good option i guess.. its definitely easy to code. However i think we can have it done more efficiently if we just perform a BFS on the given tree. and every time just check tht the left child is smaller and right chiled is greater than or equal to the current node. If we successfully search the entire tree then it is a proper BST otherwise its not.