Pergunta de entrevista da empresa Microsoft

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.

Sigiloso

29 de mar. de 2010

isBST(node n) if(n.left == null || n.right == null) return true; if(n.left.val < n.right.val) return isBST(n.left) && isBST(n.right) else return false;

1

Sigiloso

1 de fev. de 2010

BST(Node * p) { if (p==null) return false; if(p->left! null& p->dataleft) return false if(p->right&&p->data>=max(p->right)) return false if(!BST(p->left) && BST(p->right)) return true return false; }