Write an algorithm to verify if a tree is a binary search tree.
Réponses aux questions d'entretien
Utilisateur anonyme
22 févr. 2010
This solution is wrong.
if (p==null) return false; - Why is this false?
1
Utilisateur anonyme
16 déc. 2010
This solution is also false. It's going to return true if there is no right node and the left node is greater than the parent. It's basically right, and is a simple fix to change that, so I'll leave it to the reader as an exercise. :)
Utilisateur anonyme
30 déc. 2012
Assume that we have a binary search tree like this:
RootNode has value 6.
RootNode->LeftChild has value 3.
RootNode->LeftChild->LeftChild has value 2.
RootNode->LeftChild->RightChild has value 7.
We know that in a binary tree
1.all nodes' values which are at left side of a node must be less than Node's value.
2.all nodes' values which are at right side of a node must be greater than Node's value.
In anonymous algorithm it does not check:
1. if node's left child's value is less than node's value && node's right child's value is greater than node's value
2. node's all left childrens' values must be less than node's value.