代码随想录算法训练营第十五天|二叉树-层序遍历,广度优先搜索、翻转二叉树、对称二叉树

102. 二叉树的层序遍历

int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
    int** ans = (int**)malloc(sizeof(int*) * 2000);
    *returnSize = 0;
    if (!root) return NULL;
    int columnSizes[2000]; //记录每一层节点数
    struct TreeNode* queue[2000]; //模拟队列
    int rear = 0, head = 0;
    queue[rear++] = root;

    while (rear != head)
    {
        ans[(*returnSize)] = (int*)malloc(sizeof(int) * (rear-head));
        columnSizes[(*returnSize)] = rear - head;
        int start = head;
        head = rear;
        for (int i = start; i < head; i++)
        {
            ans[(*returnSize)][i-start] = queue[i]->val;
            if(queue[i]->left) queue[rear++] = queue[i]->left;
            if(queue[i]->right) queue[rear++] = queue[i]->right;
        }
        (*returnSize)++;
    }
    *returnColumnSizes = (int*)malloc(sizeof(int)*(*returnSize));
    for(int i=0; i<(*returnSize); i++) (*returnColumnSizes)[i] = columnSizes[i];
    return ans;
}

107. 二叉树的层序遍历 II

int** levelOrderBottom(struct TreeNode* root, int* returnSize, int** returnColumnSizes){

    int** ans= (int**)malloc(sizeof(int*)  * 2000);

    *returnSize = 0;

    if (!root) return NULL;

    struct TreeNode* queue[2000];

    int columnSizes[2000];

    int head = 0, rear = 0;

    queue[rear++] = root;

    while (head != rear)

    {

        ans[(*returnSize)] = (int*)malloc(sizeof(int)*(rear - head));

        columnSizes[(*returnSize)] = rear - head;

        int start = head;

        head = rear;

        for (int i=start; i<head; i++)

        {

            ans[(*returnSize)][i-start] = queue[i]->val;

            if (queue[i]->left) queue[rear++] = queue[i]->left;

            if (queue[i]->right) queue[rear++]= queue[i]->right;

        }

        (*returnSize)++;

    }

    *returnColumnSizes = (int*)malloc(sizeof(int)*(*returnSize));

    for (int i=0; i<(*returnSize); i++)

    {

        (*returnColumnSizes)[i] = columnSizes[i];

    }

    int** ret = (int**)malloc(sizeof(int*)*(*returnSize));

    int nums[*returnSize];

    for (int i=0; i<(*returnSize); i++)

    {

        nums[i] = (*returnColumnSizes)[i];

    }

    for (int i=0; i<(*returnSize); i++)

    {

        (*returnColumnSizes)[i] = nums[(*returnSize) - i - 1];

    }

    for (int i=0; i<(*returnSize); i++)

    {

        ret[i] = (int*)malloc(sizeof(int) * nums[(*returnSize) - i - 1]);// 注意申请内存时的大小不能用(*returnColumnSizes)[i]

        for (int j=0; j < nums[(*returnSize) - i - 1]; j++)

        {

            ret[i][j] = ans[(*returnSize) - i - 1][j];

        }

    }

    free(ans);

    return ret;

}

199. 二叉树的右视图

int* rightSideView(struct TreeNode* root, int* returnSize){

    *returnSize = 0;

    int* res = malloc(sizeof(int) * 101);

    struct TreeNode* queue[101];

    int front, last, rear;

    front = rear = 0;

    queue[root ? rear++ : rear] = root;

    last = rear;

    while (front < rear)

    {

        if (queue[front]->left) queue[rear++] = queue[front]->left;

        if (queue[front]->right) queue[rear++] = queue[front]->right;

        front++;

        if (front == last)

        {

            res[(*returnSize)++] = queue[last-1]->val;

            last = rear;

        }

    }

    return res;

}

637. 二叉树的层平均值

double* averageOfLevels(struct TreeNode* root, int* returnSize){

    double* average = malloc(sizeof(double) * 10000); //肯定不能使用完全二叉树的算式,最大容量为15肯定出错,因为你不知道这个二叉树长啥样子

    struct TreeNode** queue = malloc(sizeof(struct TreeNode*) * 10000);

    *returnSize = 0;

    int front = 0, rear = 0;

    queue[rear++] = root;

    int start = 0;

    while (front < rear)

    {

        int size = rear - front;

        start = front;

        front = rear;

        double sum = 0;

        for (int i=start; i<front; i++)

        {

            sum += queue[i]->val;

            if (queue[i]->left) queue[rear++] = queue[i]->left;

            if (queue[i]->right) queue[rear++] = queue[i]->right;

        }

        average[(*returnSize)++] = sum / size;

    }

    return average;

}

429. N 叉树的层序遍历

int** levelOrder(struct Node* root, int* returnSize, int** returnColumnSizes) {

    int** ans = (int**)malloc(sizeof(int*)*1000);

    *returnColumnSizes = (int*)malloc(sizeof(int)*1000);

    *returnSize = 0;

    if (!root) return ans;

    struct Node** queue = (struct Node**)malloc(sizeof(struct Node*)*10000);

    int head = 0, tail = 0;

    int level = 0;

    queue[tail++] = root;

    while (head != tail)

    {

        int cnt = tail - head;

        ans[level] = (int*)malloc(sizeof(int) * cnt);

        for (int i=0; i<cnt; i++)

        {

            struct Node* cur =queue[head++];

            ans[level][i] = cur->val;

            for (int j=0; j<cur->numChildren; j++)

            {

                queue[tail++] = cur->children[j];

            }

        }

        (*returnColumnSizes)[level++] = cnt;

    }

    *returnSize = level;

    free(queue);

    return ans;

}

 515. 在每个树行中找最大值

int* largestValues(struct TreeNode* root, int* returnSize){

    int* res = (int*)malloc(sizeof(int) * 10001);

    int pos = 0;

    *returnSize = 0;

    if (!root) return res;

    struct TreeNode** queue = (struct TreeNode**)malloc(sizeof(struct TreeNode)*10001);

    int head = 0, tail = 0;

    queue[tail++] = root;

    while (head != tail)

    {

        int maxVal = INT_MIN;

        int len = tail - head;

        int start = head;

        head = tail;

        for (int i=start; i<head; i++)

        {

            maxVal = maxVal > queue[i]->val ? maxVal : queue[i]->val;

            if (queue[i]->left) queue[tail++] = queue[i]->left;

            if (queue[i]->right) queue[tail++] = queue[i]->right;

        }

        res[pos++] = maxVal;

    }

    *returnSize = pos;

    free(queue);

    return res;

}

116. 填充每个节点的下一个右侧节点指针

struct Node* connect(struct Node* root) {

    if (!root) return NULL;

    struct Node* leftmost = root;

    // 每一次指定每一层的第一个节点

    while (leftmost->left)

    {

        struct Node* head = leftmost;

        while (head)

        {

            head->left->next = head->right;

            head->right->next = head->next ? head->next->left : NULL;

            head = head->next;

        }

        leftmost = leftmost->left;

    }

    return root;

}

117. 填充每个节点的下一个右侧节点指针 II

// 在一层中找,直到最后一个节点

struct Node *find_next(struct Node *root)

{

    while (root)

    {

        if (root->left) return root->left;

        if (root->right) return root->right;

        root = root->next;

    }

    return NULL;

}

struct Node* connect(struct Node* root) {

    if (!root) return NULL;

    struct Node* leftmost = root;

    while (find_next(leftmost))

    {

        struct Node* head = leftmost;

        while (head)

        {

            if (head->right) head->right->next = find_next(head->next);

            if (head->left) head->left->next = head->right ? head->right : find_next(head->next);

            head = head->next;

        }

        leftmost = find_next(leftmost);

    }

    return root;

}

104. 二叉树的最大深度

int maxDepth(struct TreeNode* root){

    int maxVal = 0;

    if (!root) return 0;

    struct TreeNode** queue = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 10001);

    int head = 0, tail = 0;

    queue[tail++] = root;

    while (head != tail)

    {

        int n = tail - head;

        int start = head;

        head = tail;

        for (int i=start; i<head; i++)

        {

            if (queue[i]->left) queue[tail++] = queue[i]->left;

            if (queue[i]->right) queue[tail++] = queue[i]->right;

        }

        maxVal++;

    }

    free(queue);

    return maxVal;

}

111. 二叉树的最小深度

// 1

int minDepth(struct TreeNode* root){

    if (!root) return 0;

    struct TreeNode* queue[10000];

    struct TreeNode* cur;

    int count = 0;

    int head = 0, tail = 0;

    queue[tail++] = root;

    while (head != tail)

    {

        int len = tail - head;

        count++;

        for (int i=0; i<len; i++)

        {

            cur = queue[head++];

            if (!cur->left && !cur->right) return count;

            if (cur->left) queue[tail++] = cur->left;

            if (cur->right) queue[tail++] = cur->right;

           

        }

    }

    return count;

}

 // 2

int minDepth(struct TreeNode* root){
    if (!root) return 0;
    struct TreeNode** queue = (struct TreeNode**)malloc(sizeof(struct TreeNode*)* 100001);
    int count = 0;
    int head = 0, tail = 0;
    queue[tail++] = root;
    while (head != tail)
    {
        int len = tail - head;
        int start = head;
        head = tail;
        count++;
        for (int i=start; i<head; i++)
        {
            if (queue[i]->left) queue[tail++] = queue[i]->left;
            if (queue[i]->right) queue[tail++] = queue[i]->right;
            if (!queue[i]->left && !queue[i]->right) return count;
        }
    }
    return count;
}

226. 翻转二叉树 

递归

struct TreeNode* invertTree(struct TreeNode* root){

    if (!root) return NULL;

    struct TreeNode* tmp = root->left;

    root->left = root->right;

    root->right = tmp;

    invertTree(root->left);

    invertTree(root->right);

    return root;

}

层序遍历

struct TreeNode* invertTree(struct TreeNode* root){

    if (!root) return NULL;

    struct TreeNode* queue[101];

    int head = 0, tail = 0;

    queue[tail++] = root;

    while (head < tail)

    {

        int start = head;

        head = tail;

        for (int i=start; i<head; i++)

        {

            struct TreeNode* tmp = queue[i]->left;

            queue[i]->left = queue[i]->right;

            queue[i]->right = tmp;

            if (queue[i]->left) queue[tail++] = queue[i]->left;

            if (queue[i]->right) queue[tail++] = queue[i]->right;

        }

    }

    return root;

}

 

101. 对称二叉树 

bool compare(struct TreeNode* left, struct TreeNode* right)

{

    if (left == NULL && right == NULL) return true;

    if (left == NULL || right == NULL) return false;

    if (left->val != right->val) return false;

    // 在节点都存在的情况下,比较左子树的左节点和右子树的右节点是否相同,比较左子树的右节点和右子树的左节点是否相同

    return compare(left->left, right->right) && compare(left->right, right->left);

}

bool isSymmetric(struct TreeNode* root){

    if (!root) return true;

    return compare(root->left, root->right);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

is_xiaotian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值