代码随想录算法训练营第18天 | 102. 二叉树的层序遍历 | 226. 翻转二叉树 | 101. 对称二叉树

102. 二叉树的层序遍历

题目链接

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */

const int max = 10000;

struct queue {
    int front, back;
    struct TreeNode *array[10000];
};

int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {
    struct queue *que = (struct queue *)malloc(sizeof(*que));
    struct TreeNode *node = root;
    int **ans = (int **)malloc(sizeof(int*) * max);
    int row = 0, col;
    *returnColumnSizes = (int *)malloc(sizeof(int) * max);

    memset(que, 0, sizeof(*que));
    que->front = que->back = 0;
    memset(ans, 0, sizeof(int*) * max);

    *returnSize = 0;

    if (root == NULL) {
        return ans;
    }

    que->array[que->back++] = node;

    while (que->back != que->front) {
        col = 0;
        ans[row] = (int *)malloc(sizeof(int) * max);
        memset(ans[row], 0, sizeof(int) * max);

        int cur = que->back;
        while (cur != que->front) {
            node = que->array[que->front];
            ans[row][col++] = node->val;

            if (node->left != NULL) que->array[que->back++] = node->left;
            if (node->right != NULL) que->array[que->back++] = node->right;
            
            que->front++;
        }

        (*returnColumnSizes)[row] = col;

        row++;
    }

    *returnSize = row;
    
    return ans;
}

226. 翻转二叉树

题目链接

解: 递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* invertTree(struct TreeNode* root) {
    if (root == NULL) return root;
    struct TreeNode *tmp = root->left;
    root->left = root->right;
    root->right = tmp;
    invertTree(root->left);
    invertTree(root->right);

    return root;
}

101. 对称二叉树

题目链接

解: 递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

bool compare(struct TreeNode *left, struct TreeNode *right) {
    if (left == NULL && right != NULL) return false;
    else if (left != NULL && right == NULL) return false;
    else if (left == NULL && right == NULL) return true;
    else if (left->val != right->val) return false;

    bool outside = compare(left->left, right->right);
    bool inside = compare(left->right, right->left);

    return outside && inside;
}

bool isSymmetric(struct TreeNode* root) {
    if (root == NULL) return true;

    return compare(root->left, root->right);
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值