码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode //C - 637. Average of Levels in Binary Tree


    637. Average of Levels in Binary Tree

    Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 1 0 − 5 10^{-5} 10−5 of the actual answer will be accepted.
     

    Example 1:

    在这里插入图片描述

    Input: root = [3,9,20,null,null,15,7]
    Output: [3.00000,14.50000,11.00000]
    Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
    Hence return [3, 14.5, 11].

    Example 2:

    在这里插入图片描述

    Input: root = [3,9,20,15,7]
    Output: [3.00000,14.50000,11.00000]

    Constraints:
    • The number of nodes in the tree is in the range [ 1 , 1 0 4 1, 10^4 1,104].
    • − 2 31 < = N o d e . v a l < = 2 31 − 1 -2^{31} <= Node.val <= 2^{31} - 1 −231<=Node.val<=231−1

    From: LeetCode
    Link: 637. Average of Levels in Binary Tree


    Solution:

    Ideas:
    1. Initialize a queue to store nodes at the current level.

    2. Add the root node to the queue.

    3. While there are nodes in the queue:
      a. Record the number of nodes at the current level (i.e., the size of the queue).

      b. Initialize two variables: sum for the sum of node values at the current level, and count for the number of nodes.

      c. For each node at the current level:

      • i. Dequeue the node.
      • ii. Add its value to sum.
      • iii. Enqueue its left child and right child (if they exist).
      • iv. Increment count.

      d. Compute the average for the current level by dividing sum by count. Add this average to the result list.

    4. Return the result list.

    Code:
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    double* averageOfLevels(struct TreeNode* root, int* returnSize){
        // Edge case: if the tree is empty
        if (!root) {
            *returnSize = 0;
            return NULL;
        }
        
        struct TreeNode** queue = (struct TreeNode**)malloc(10000 * sizeof(struct TreeNode*));
        double* result = (double*)malloc(10000 * sizeof(double));
        int front = 0, rear = 0, levelCount, resultIndex = 0;
        double sum;
        
        queue[rear++] = root; // enqueue root
        
        while (front < rear) {
            levelCount = rear - front; // nodes at current level
            sum = 0;
            for (int i = 0; i < levelCount; i++) {
                struct TreeNode* currentNode = queue[front++];
                sum += currentNode->val;
                if (currentNode->left) queue[rear++] = currentNode->left;
                if (currentNode->right) queue[rear++] = currentNode->right;
            }
            result[resultIndex++] = sum / levelCount;
        }
        
        *returnSize = resultIndex;
        free(queue);
        return result;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
  • 相关阅读:
    stack-es-标准篇-ElasticsearchClient-function_score
    2013 ~【VUE+ ElementUI】——【上传、下载】进度计算
    【html】做设计需要熟悉的一个颜色值列表
    全网独我一份,SpringCloud Alibaba手打笔记
    物联网开发笔记(48)- 使用Micropython开发ESP32开发板之控制OLED ssd1306屏幕
    电源升压模块dc/dc直流低压升高压隔离电压变换器5v12v24v48v转50v70v80v100v110v200v250v300v500v600v微功率
    Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama
    SpringCloudAlibaba系列微服务搭建笔记五_Dubbo
    第二十三章 CSP Session 管理 - 身份验证共享策略
    搭建lnmp+nfs+调度器
  • 原文地址:https://blog.csdn.net/navicheung/article/details/132894322
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号