Share my C code (0ms) , using Queue.


  • 0
    Y

    i post all my code including the driver code.

    #include <stdio.h>
    #include <string>
    
    struct TreeNode{
        int val;
        struct TreeNode* left;
        struct TreeNode* right;
    
    };
    
    struct Node{
        struct TreeNode* data;
        struct Node* next;
    };
    
    typedef struct Queue{
        struct Node* front;
        struct Node* tail;
     } Queue;
    
    void init(Queue* queue){
        queue->front=NULL;
        queue->tail=NULL;
    }
    
    void enQueue(Queue* queue,struct TreeNode* data){
        if(queue->front==NULL){
               queue->front=(struct Node*)malloc(sizeof(struct Node));
               queue->front->data=data;
               queue->front->next=NULL;
               queue->tail=queue->front;
        }
        else{
                queue->tail->next=(struct Node*)malloc(sizeof(struct Node));
                queue->tail->next->data=data;
                queue->tail->next->next=NULL;
                queue->tail=queue->tail->next;
        }
     }
    
     struct TreeNode* deQueue(Queue* queue){
        if(queue->front==NULL)
                return NULL;
        else{
                struct Node* del=queue->front;
                struct TreeNode* data =del->data;
                queue->front=queue->front->next;
                free(del);
                return data;
        }
     }
    
     bool empty(Queue* queue){
        if(queue->front==NULL)
                return true;
        return false;
     }
     int* rightSideView(struct TreeNode* root, int* returnSize) {
        Queue* queue =(Queue*)malloc(sizeof(Queue));
        init(queue);
        if(root == NULL){
                *returnSize=0;
                return NULL;
        }
        int* result =(int*)malloc(sizeof(int)*128);
        int size=0;
        enQueue(queue,root);
        enQueue(queue,NULL);
        struct TreeNode* pre=NULL,*temp=NULL;
        while(!empty(queue)){
                temp=deQueue(queue);
                if(temp){
                        if(temp->left)
                                enQueue(queue,temp->left);
                        if(temp->right)
                                enQueue(queue,temp->right);
                }
                else{
                        result[size++]=pre->val;
                        if(empty(queue))
                                break;
                        enQueue(queue,NULL);
                }
                pre=temp;
        }
      
        *returnSize=size;
        return result;
      }
    
    struct TreeNode* buildTree(){
        int val;
        scanf("%d",&val);
        if(val!=-1){
                struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
                root->left=buildTree();
                root->right=buildTree();
                root->val=val;
        }
        else
                return NULL;
     }
    
     int main()
    {
    struct TreeNode* root=buildTree();
    int size;
    rightSideView(root,&size);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.