Microsoft Interview Question
Country: India
Interview Type: In-Person
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
hey your ques are coming couple of times .... please remove the duplicate ones.... thanks a lot
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;
}
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)));
}
}
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;
}
}
You can watch learn how to delete all the nodes of a binary search tree from this wonderful video tutorial
youtu.be/qZbmY-2Y26Y
delete the nodes in postorder traversal.
- Anonymous June 30, 2012