题解 | #从上往下打印二叉树#
从上往下打印二叉树
https://www.nowcoder.com/practice/7fe2212963db4790b57431d9ed259701
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; }