Bloomberg Interview Question

Write a program that verifies that a binary tree is a binary search tree.

Interview Answers

Anonymous

Jan 16, 2014

Let the BinaryTree contain 3 variables: value, leftChild, rightChild. The leftChild and the rightChild are again type of BinaryTree only. Now say you are given root node as the argument. At every stage, check if the value of the leftChild is less than current node value which is less than value of rightChild. If it is satisfied, recurse through its children. Otherwise, return "Not a Binary Search Tree"

Anonymous

Jan 20, 2014

void traversal(Node* root){ if(*root== NULL) return; ArrayList thislevel = new ArrayList; thislevel.add(root) ; while(!thislevel.isempty()){ArrayList nextlevel = new ArrayList; for(Node s in thislevel) { if(s= = NULL)break;if(s>s.leftchild)if(s.rightchild>s){nextlevel.add(left);nextlevel.add(right);}else{return(0);)} thislevel = nextlevel;}return(1);}

2