Tuesday 12 November 2013

Some Useful Data structure interview questions

Remove the leave Node

struct Node * romoveLeafList(struct Node * root){
    if(root==NULL) return NULL;
    if(root->left==NULL && root->right==NULL){
        return NULL;
    }
    root->left=romoveLeafList(root->left);
    root->right=romoveLeafList(root->right);
    return root;
 
}


Remove all the Node which has sum is less than k 

struct Node * sumofnode(struct Node *root, int k, int *sum)
{
    if(root==NULL)return NULL;
    int leftsum=*sum+root->data;
    int rightsum=leftsum;
    root->left=sumofnode(root->left,k,&leftsum);
    root->right=sumofnode(root->right,k,&rightsum);
    *sum=leftsum>rightsum?leftsum:rightsum;
    if(*sum<k)
    return NULL;
    return root;
}

No comments:

Post a Comment