leetcode-637. Average of Levels in Binary Tree (C语言,层次遍历,队列实现)

637. Average of Levels in Binary Tree

Method:层次遍历,将每一层的所有TreeNode都存储在一个临时队列Queue里面,再循环遍历每一层。用c语言的话需要自己创建队列Queue,即一个Queue中的每个QNode是TreeNode类型的,还要实现它的enQueue操作。

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

//new queue
typedef struct QNode {
    struct TreeNode *t_node;
    struct QNode *next;
} QNode;

typedef struct Queue {
    QNode *head;
    QNode *tail;
} Queue;

Queue *initQueue() {
    Queue *q = (struct Queue *)malloc(sizeof(Queue));
    q->head = q->tail = NULL;
    return q;
}

void enQueue(struct Queue *q, struct TreeNode *t_node) {
    QNode *q_node = (struct QNode *)malloc(sizeof(QNode));
    q_node->t_node = t_node;
    q_node->next = NULL; //这个next一定要赋值!!否则编译时会报错 Line 91: member access within misaligned address 0x756e2c3931382d2c for type 'struct TreeNode', which requires 8 byte alignment
    if (!q->head) {
        q->head = q_node;
        q->tail = q_node;
    } else {
        q->tail->next = q_node;
        q->tail = q_node;
    }
}

QNode *outHeadQueue(struct Queue **q) {
    struct Queue *tmp = *q;
    if (!(tmp->head)) {
        return NULL;
    } else {
        struct QNode *out_qNode = tmp->head;
        tmp->head = tmp->head->next;
        return out_qNode;
    }
}

int isEmpty(struct Queue *q) {
    if (!q->head) {
        return 1;
    } else {
        return 0;
    }
}

int findMaxDepth(struct TreeNode *root) {
    if (!root) {
        return 0;
    }
    if (!root->left && !root->right) {
        return 1;
    }
    int left = findMaxDepth(root->left);
    int right = findMaxDepth(root->right);
    return (left > right) ? (left + 1) : (right + 1);
}

double* averageOfLevels(struct TreeNode* root, int* returnSize) {
    int i = 0;
    int maxDepth = findMaxDepth(root);
    double *ret = (double *)malloc(sizeof(double) * maxDepth);
    *returnSize = maxDepth;
    printf("sizeof maxDepth=%d\n", maxDepth);
   
    struct Queue *q = initQueue();
    enQueue(q, root);
    while (!isEmpty(q)) {
    //for (i = 0; i < maxDepth;) { //对于外循环,这两个判断条件都可以用
        struct Queue *next_q = initQueue(); //分配临时的Queue用来装某一层所有的TreeNode
        double sum = 0, ct = 0;
        while (!isEmpty(q)) {
            QNode *tmp = outHeadQueue(&q); //注意这里要修改原q(去除其头部节点),因此要传入它的地址。
            sum += tmp->t_node->val;
            ct++;
            if (tmp->t_node->left) {
                enQueue(next_q, tmp->t_node->left);
            }
            if (tmp->t_node->right) {
                enQueue(next_q, tmp->t_node->right);
            }
        }

        q = next_q;
        ret[i++] = sum/ct;
        printf("ret[i]=%lf,i=%d\n", ret[i-1], i-1);
    }
    return ret;
}

Submission Detail

65 / 65 test cases passed.

Status: 

Accepted

Runtime: 8 ms

Submitted: 4 minutes ago

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值