Write an algorithm to verify if a tree is a binary search tree.
Respostas da entrevista
Sigiloso
22 de fev. de 2010
This solution is wrong.
if (p==null) return false; - Why is this false?
1
Sigiloso
16 de dez. de 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. :)
Sigiloso
30 de dez. de 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.