Microsoft Interview Question


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 vote

delete the nodes in postorder traversal.

- Anonymous June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can watch and understand the code to delete all the nodes of a binary search tree from this amazing video tutorial
youtu.be/qZbmY-2Y26Y

- Anonymous June 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good effort. Answer is: Post Order traversal and delete the node where you would usually print it

- Sandeep July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

hey your ques are coming couple of times .... please remove the duplicate ones.... thanks a lot

- student June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am very sorry.. I have clicked submit button twice. But now I am unable delete the duplicate questions.

- gdb June 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

also where all this ques in written test or interview....

- student June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

delete the nodes in post order traversal..

- Anonymous June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String args[])
{
  Read Tree root T
  DeleteNodeInPostOrder(T)
  T = null;
}
public static void DeleteNodeInPostOrder(Node T)
{
  if(T == null)
    return;
  DeleteNodeInPostOrder(T.left);
  T.Left = null;
  DeleteNodeInPostOrder(T->right);
  T.right = null;
}

- Randomness June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Where are your nodes deleted?
You are simply setting them to NULL. Effectively leaking memory.

- Pavitra.Ghosh July 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void deleteBst(struct node** node){
     if (*node == NULL)
        return;
    else if ( !((*node)->left) && !( (*node)->right) )) {
        free(*node);
        *node=NULL;
    }
    else {
       deleteBst( &((*node)->left)));
       deleteBst( &((*node)->right)));
    }

}

- coolsolution June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, last solution is little wrong this is the correct working code

void deleteBst(struct node** node){
    
     if (*node == NULL)
        return;
    
     printf("traversing %d node now\n",(*node)->data);
     
     deleteBst( &((*node)->left));
     deleteBst( &((*node)->right));
     
     if ( !((*node)->left) && !( (*node)->right) ) {
        printf("deleting node %d now\n",(*node)->data);
        free(*node);
        *node=NULL;
     }
    
    
}

- coolsolution June 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can watch learn how to delete all the nodes of a binary search tree from this wonderful video tutorial
youtu.be/qZbmY-2Y26Y

- Anonymous June 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In your code, do we actually need the condition to free the node..? Won`t it work without the condition..? Please explain me if I am wrong..!

if ( !((*node)->left) && !( (*node)->right) ) {
   // free the node
}

- f2003062 July 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Note: After deleting the BST make root=NULL

void deleteBST(root)
{
	if(root)
	{
		deleteBST(root->left);
		deleteBST(root->right);

		free(root);
                root=NULL;
	}
}

- Aashish June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{
delete_bst(bst_node *root) {
if (root) {
delete_bst(root->left);
delete_bst(root->right);
delete(root);
}
}}

- xx June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

test(){

   }

- test June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Non recursive approach could be DFT (Depth First Search) and delete nodes from top of stack...

- TS July 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we not do it using inorder, preorder traversals too?

- Anonymous July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Post order traversal only works because while deleting the nodes, first delete the childs (i.e. left and right nodes) and then delete the root itself..

- f2003062 July 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Perform BFS-

void modified_bfs(node *root)
{
	if( root == 0 )
		return;
	q.add(root);
	while( !q.empty() )
	{
		node* x = q.delete();
		if( x->left )
			q.add(x->left);
		if( x->right )
			q.add(x->right)
		delete x; /*free x */
	}
}

- confused_banda December 25, 2013 | Flag Reply


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