题解 | #从上往下打印二叉树#

从上往下打印二叉树

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;
}

全部评论

相关推荐

大飞的诡术妖姬:之前看b站多明海有个说法,日本就业竞争非常低的原因不光是毕业学生少,还有很多人干两年不喜欢职场氛围就辞职躺平,位置也空了很多,论吃苦耐劳还得看咱们
点赞 评论 收藏
分享
仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务