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


    LeetCode-637. Average of Levels in Binary TreeLevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/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 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, 10^4].
    • -2^31 <= Node.val <= 2^31 - 1

    【C++】

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. vector<double> averageOfLevels(TreeNode* root) {
    15. vector<double> res;
    16. if (root == nullptr) {return res;}
    17. queue q;
    18. q.push(root);
    19. while (!q.empty()) {
    20. int size = q.size(), count = size;
    21. double sum = 0;
    22. while (size-- > 0) {
    23. TreeNode* node = q.front();
    24. q.pop();
    25. sum += node->val;
    26. if (node->left) {q.push(node->left);}
    27. if (node->right) {q.push(node->right);}
    28. }
    29. res.push_back(sum / count);
    30. }
    31. return res;
    32. }
    33. };

    【Java】

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public List averageOfLevels(TreeNode root) {
    18. List res = new ArrayList<>();
    19. if (root == null) {return res;}
    20. Queue q = new LinkedList<>();
    21. q.offer(root);
    22. while (!q.isEmpty()) {
    23. int size = q.size(), count = size;
    24. double sum = 0;
    25. while (size-- > 0) {
    26. TreeNode node = q.poll();
    27. sum += node.val;
    28. if (node.left != null) {q.offer(node.left);}
    29. if (node.right != null) {q.offer(node.right);}
    30. }
    31. res.add(sum / count);
    32. }
    33. return res;
    34. }
    35. }

  • 相关阅读:
    hdlbits系列verilog解答(向量门操作)-14
    【操作系统】文件管理(一)—— 文件管理的概述
    Node学习五(2) —— 文件操作(fs模块)
    批量多字段唯一性校验
    【网络安全必看】如何提升自身WEB渗透能力?
    CS224W2.2——传统基于特征的方法(边层级特征)
    Programming tools
    登录注册页面的模拟
    10个基于.Net开发的Windows开源软件项目
    王道书 P150 T12(打印x的所有祖先) + 拓展(打印从根节点到某个节点的路径、求根节点到某个节点的路径长度、求根节点的最大路径长度)
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126321340
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号