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

从上往下打印二叉树

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

全部评论

相关推荐

点赞 评论 收藏
分享
想按时下班的我在等o...:我投测试也是这个情况,不知道咋办了
点赞 评论 收藏
分享
字节一直是我的白月光,考虑到转正还是拒了日常实习。
从今天开始狠狠卷JV...:为什么你释放的offer没流到我头上
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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