/**
 * struct TreeNode {
 *  int val;
 *  struct TreeNode *left;
 *  struct TreeNode *right;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param t1 TreeNode类
 * @param t2 TreeNode类
 * @return TreeNode类
 */
#include<stdlib.h>
#include<stdbool.h>
typedef struct queue {
	struct TreeNode* data[500];
	int size;
}queue;
queue* init_queue() {
	queue* my_queue = (queue*)malloc(sizeof(queue));
	if (my_queue == NULL) {
		return NULL;
	}
	for (int i = 0; i < 500; i++) {
		my_queue->data[i] = NULL;
	}
	my_queue->size = 0;
	return my_queue;
}
void push_queue(queue* my_queue, struct TreeNode* data) {
	if (my_queue == NULL) {
		return;
	}
	my_queue->data[my_queue->size] = data;
	my_queue->size++;
}
struct TreeNode* pop_queue(queue* my_queue) {
	if (my_queue == NULL||my_queue->size==0) {
		return NULL;
	}
	struct TreeNode* value = my_queue->data[0];
	for (int i = 0; i < my_queue->size-1; i++) {
		my_queue->data[i] = my_queue->data[i+1];
	}
	my_queue->size--;
	return value;
}
bool isEmpty(queue* my_queue) {
	if (my_queue == NULL || my_queue->size == 0) {
		return true;
	}
	return false;
}
struct TreeNode* mergeTrees(struct TreeNode* t1, struct TreeNode* t2) {
	// write code here
	queue* my_queue1 = init_queue();
	queue* my_queue2 = init_queue();
	if (t1 == NULL) {
		return t2;
	}
	if (t2 == NULL) {
		return t1;
	}
	if (t1 != NULL && t2 != NULL) {
		t1->val += t2->val;
		push_queue(my_queue1, t1);
		push_queue(my_queue2, t2);
	}
	while (!isEmpty(my_queue1)&&!isEmpty(my_queue2)) {
		struct TreeNode* tem = malloc(sizeof(struct TreeNode));
		struct TreeNode* cur1 =pop_queue(my_queue1);
		struct TreeNode* cur2 =pop_queue(my_queue2);
		if (cur1->left != NULL) {
			if (cur2->left != NULL) {
				cur1->left->val += cur2->left->val;
				push_queue(my_queue2, cur2->left);
				push_queue(my_queue1, cur1->left);
			}
		}
		else {
			if (cur2->left != NULL) {
				tem= cur2->left;
				cur1->left = tem;
			}
		}
		if (cur1->right != NULL) {
			if (cur2->right != NULL) {
				cur1->right->val += cur2->right->val;
				push_queue(my_queue2, cur2->right);
				push_queue(my_queue1, cur1->right);
			}
		}
		else {
			if (cur2->right != NULL) {
				tem= cur2->right;
				cur1->right = tem;
			}
		}
	}
	return t1;
}