Interview Question






Comment hidden because of low score. Click to expand.
0
of 2 vote

What does it mean by no extra space? Does it mean that we should not use any extra space for creating the BST nodes, rather the BST nodes are already given the pre-order list, we just have to set the left and right childs properly.

Or is it that except for creating BST nodes we should not use any extra space?

- a.khan September 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here is a more concrete version. Given is a class

class Node {
    Node left;
    int value;
    Node right;
}

You must write a method

Node makeBST(Node[] preorder);

that makes a BST and returns the root. makeBST must not use new or call any methods (including itself, so no recursion).

- Anonymous September 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The nodes in preorder have value fields initialized but null left and right.

- Anonymous September 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess there's not much point in requiring the root to be returned, but w/e.

- Anonymous September 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Some thoughts
1. Sort the elements to get the Inorder (Remember that a BST sorted is same as its in order)
2. Use the Inorder and Pre/Post order to make the tree. This is discussed many times.

- LostInTime September 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How do you do this with no extra space?

- Anonymous September 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It Can't be solved with only one order ..
It must given ( Inorder and Pre/Post-order ).

- Anonymous September 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

BST = Binary SEARCH Tree.

- Anonymous September 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

To convert a preorder traversal to BST, following are the conditions:
1.First element will be root.
2.LeftChild=nearest element less than root
3. RightChild=nearest element greater than root

- rash September 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not an algorithm.

- Anonymous September 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey30417" class="run-this">TreeNode* ConstructFromPreOrder(int pre[], int i, int j, int size)
{
if(i<=j)
{
TreeNode* root = new TreeNode();
root->data = pre[i];
int k = FindLeftSubtree(pre, i, j, root->data, size);
root->left = ConstructFromPreOrder(pre, i+1, k, size);
root->right = ConstructFromPreOrder(pre, k+1, j, size);
return root;
}
return NULL;
}

int FindLeftSubtree(int pre[], int i, int j, int root, int size)
{
int k = i;
for(; pre[k] <= root && k <size; ++k) ;
return k-1;
}</pre><pre title="CodeMonkey30417" input="yes">
</pre>

- Anonymous September 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort the elements to get inorder traversal sequence.
With preorder and inorder, you can construct the BST

- Hong Sun September 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
Given preorder traversal of a BST. Build the BST.
*/

#include<iostream>
#include<stdlib.h>
using namespace std;

typedef struct tree{
int val;
struct tree *left;
struct tree *right;
}tree;

tree* insert(tree* root, int val)
{
if(root == NULL)
{
tree* temp = (tree*) malloc(sizeof(tree));
temp->val = val;
temp->left = NULL;
temp->right = NULL;
return temp;
}

tree* node = (tree*) malloc(sizeof(tree));
if(root->val < val)
root->right = insert(root->right, val);
else
root->left = insert(root->left, val);
return root;
}

tree* buildBST(int preOrder[], int length)
{
tree* root;
if(length != 0)
{
root = (tree*) malloc(sizeof(tree));
root->val = preOrder[0];
root->left = NULL;
root->right = NULL;
}
else
return NULL;

for(int i = 1; i < length; i++)
{
root = insert(root, preOrder[i]);
}

return root;
}

void printInOrder(tree* BST)
{
if(BST == NULL) return;
printInOrder(BST->left);
cout << BST->val << " ";
printInOrder(BST->right);
}

void printPreOrder(tree* BST)
{
if(BST == NULL) return;
cout << BST->val << " ";
printPreOrder(BST->left);
printPreOrder(BST->right);
}


int main(void)
{
int preOrder[] = {10, 5, 3, 4, 6, 11, 13, 12};
tree* BST = buildBST(preOrder, sizeof(preOrder) / sizeof(int));
printInOrder(BST);
cout << endl;
printPreOrder(BST);
cout << endl;
}

- Anonymous September 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In other words just do an insert operation into BST while traversing the preOrder array.

- Anonymous September 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This seems to be building a BST with given node values.... Any other possible solution ?

- Naveen November 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But that same preorder will make the same tree from which preorder was created.

- soumyajuit December 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

please provide any contradictory example in this context.

- soumyajuit December 04, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More