gap把我看糊涂了,写个简单的
#define MAX_SIZE 10001 int* levelOrder(struct TreeNode* root, int* returnSize){ *returnSize = 0; if (root == NULL) { return NULL; } int *ans = (int*)calloc(MAX_SIZE, sizeof(int)); struct TreeNode *queue[MAX_SIZE];//指针数组,存放一组指针 memset(queue, 0, sizeof(struct TreeNode*)); int head = 0; int tail = 0; queue[tail++] = root; while (head < tail) { struct TreeNode *tmp = queue[head++]; ans[(*returnSize)++] = tmp->val; if (tmp->left != NULL) { queue[tail++] = tmp->left; } if (tmp->right != NULL) { queue[tail++] = tmp->right; } } return ans; }