# 107. Binary Tree Level Order Traversal II (using c language with queue)

• Below is my code

``````struct queue_node{
void* ptr;
struct queue_node *next;
};

typedef struct queue_node node;
node *front = NULL;
node *rear = NULL;

void enqueue(void* p)
{
node *new_node;
new_node = (node *)malloc(sizeof(node));
if( !new_node )
{

exit(0);
}
new_node->ptr = p;
new_node->next = NULL;
if(front==NULL)
front = new_node;
else
rear->next = new_node;
rear = new_node;
}

void* dequeue()
{
node *top;
int temp;

if(front != NULL)
{
top = front;
front = front->next;
temp = top->ptr;
free(top);
return temp;
}
else
return NULL;
}

int** levelOrderBottom(struct TreeNode* root, int** columnSizes, int* returnSize) {

if(root==NULL) return NULL;
int** ret=(int**)malloc(4080*sizeof(int*));
*columnSizes=(int*)malloc(4080*sizeof(int));
enqueue(root);
ret[0]=(int*)malloc(sizeof(int));
*returnSize=0;
int next=0; //how many nodes in next level
int count=0; //how many node get in this level
int cur=1; //how many node in this level
int num=0;
while(front != NULL){
struct TreeNode* p= (struct TreeNode*) dequeue();
ret[*returnSize][count]=p->val;
count++;

if(p->left){

enqueue(p->left);
next++;

}
if(p->right){

enqueue(p->right);
next++;
}
if(cur==count){
num++;
(*columnSizes)[*returnSize] = cur;
(*returnSize)++;
ret[*returnSize]=(int*)malloc(next*sizeof(int));
cur=next;
next=count=0;
}
}

//Upside is the answer to the Binary Tree Level Order Traversal I

//Below is how i do for question Binary Tree Level Order Traversal II

int** ret1=(int**)malloc(*returnSize*sizeof(int*));
int i;
for(i=0;i<*returnSize;i++){
ret1[i]=(int*)malloc((*columnSizes)[*returnSize-i-1]*sizeof(int));
ret1[i]=ret[*returnSize-1-i];

}
return ret1;
}

``````

The output is shown as below

I really don't which part of my code have problem
For the Binary Tree Level Order Traversal I
I have got the good result but i got error in problem II
I have spent three days on it
Wish to hear some advise from you guys, thank you

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