Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
if the 1d array is inorder traversal of tree then it will be ok but what if the array given is either preorder or postorder traversal of the tree , how to handle them ???
Here you presume that a[] is sorted. if it is not, you cannot simply take the quarters of the matrix. but the logic remains the same:
find the treeroot in a[1,n]
then look for elements that have the same value. id a[i,j]==a[1,n], it means that ai and aj are in different subtrees. one of them is smaller than a[1,n], it will be in the left subtree, the other is bigger and will be in the right subtree. this way you will have the nodesets of the two subtrees.
solve recursively for the two subtree sets.
simple one,
lets say size of a[] is 'm'
just get the element number arr[0][m] , this element is the root of the BST.
now read the a[] one by one and keep on adding the elements in BST in normal format.
basic rule for BST is , left elemt < root element < right element.
~Rohan
Here is my sample data:
int a[] = {1,2,3,4,5,6,7,8,9,10};
int arr[10][10] =
{{0,0,0,2,0,0,0,0,0,4},
{0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,3,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,5,0,0,0,6},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,7},
{0,0,0,0,0,0,0,0,0,8},
{0,0,0,0,0,0,0,0,0,9}};
Which should built a BST like this:
5
/ \
3 7
/ \ / \
1 4 6 8
\ \
2 9
\
10
And here is my c++ code:
#include <iostream>
const int SIZE = 10;
int a[] = {1,2,3,4,5,6,7,8,9,10};
int arr[SIZE][SIZE] =
{{0,0,0,2,0,0,0,0,0,4},
{0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,3,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,5,0,0,0,6},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,7},
{0,0,0,0,0,0,0,0,0,8},
{0,0,0,0,0,0,0,0,0,9}};
class BST
{
private:
struct node {
node *right;
node *left;
int data;
};
node* root;
public:
BST()
{
root = NULL;
}
void printInOrder();
void inorder(node *);
void insert(int);
void buildBST(int, int);
};
void BST::insert(int i)
{
node *n = new node;
n->left = n->right = NULL;
n->data = i;
if( root == NULL ) {
root = n;
}
else {
node *parent = NULL;
node *curr = root;
while( curr ) {
parent = curr;
if( n->data > curr->data ) {
curr = curr->right;
}else if( n->data <= curr->data ) {
curr = curr->left;
}else {
break;
}
}
if( n->data < parent->data ) {
parent->left = n;
}else if( n->data > parent->data ) {
parent->right = n;
}
}
}
void BST::printInOrder()
{
if( root == NULL ) {
return;
}
inorder(root);
}
void BST::inorder(node *n)
{
if( n == NULL ) {
return;
}
if( n->left != NULL ) {
inorder(n->left);
}
std::cout << n->data << " ";
if( n->right != NULL ) {
inorder(n->right);
}
}
void BST::buildBST( int s, int e ) {
int i = arr[s][e];
insert( a[i] );
if( s <= i-1 ) {
buildBST( s, i-1 );
}
if( i+1 <= e ) {
buildBST( i+1, e );
}
}
int main()
{
BST b;
b.buildBST( 0, SIZE - 1 );
b.printInOrder();
return 0;
}
Assumptions made in my solution:
-- A[] from i to j all values in the tree rooted at a[i][j] INCLUDING a[i][j] itself.
--A[] is sorted.
Steps:
1) Find a[i][j] in A[]--this index will be called idx
2) Everything from i to idx-1 is the left subtree of a tree rooted at a[i][j].
3)Everything from idx+1 to j is the right subtree of a tree rooted at a[i][j]
public TreeNode getTree(int[] a, int[][] arr,int i, int j){
return buildTree(i,j,a);
}
private TreeNode buildTree(int start,int end, int[] values){
if(start>end){
return null;
}
int mid=(start+end)/2;
Node n=new Node(values[mid]);
n.left=buildTree(start,mid-1,values);
n.right=buildTree(mid+1,end,values);
return n;
}
If a[] is the inorder traverse of BST, which means a[] is sorted, and arr[][] gives the root of each subtree, we may rebuild the BST by divide and conquer:
arr[0][a.length-1] gives the root of the BST, locate it in a[] as a[k],
we divide the problem into two smaller ones:
(1)rebuild left BST with a[0, k-1] and arr[][]
(2)rebuild right BST with a[k+1, n-1] and arr[][]
Below is code in Java:
- uuuouou May 02, 2014