力扣——二叉树的层平均值

给定一个非空二叉树, 返回一个由每层节点平均值组成的数组.

示例 1:

输入:
    3
   / \
  9  20
    /  \
   15   7
输出: [3, 14.5, 11]
解释:
第0层的平均值是 3,  第1层是 14.5, 第2层是 11. 因此返回 [3, 14.5, 11].

注意:

节点值的范围在32位有符号整数范围内。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/average-of-levels-in-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
 public List<Double> averageOfLevels(TreeNode root) {
        List<Double> doubles = new ArrayList<>();
        if (root == null) return doubles;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            double ave = 0;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.add(node.left);
                if (node.right != null) queue.add(node.right);
                ave += node.val;
            }
            ave = ave * 1.0d / size;
            doubles.add(ave);
        }
        return doubles;
    }

}

原文地址:https://www.cnblogs.com/JAYPARK/p/11283030.html

时间: 2024-07-29 16:37:12

力扣——二叉树的层平均值的相关文章

LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)

637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目描述 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3 / 9 20 / 15 7 输出: [3, 14.5, 11] 解释: 第 0 层的平均值是 3,第 1 层是 14.5,第 2 层是 11.因此返回 [3, 14.5, 11]. 注意: 节点值的范围在32位

637. 二叉树的层平均值

给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3 / \ 9 20 / \ 15 7输出: [3, 14.5, 11]解释:第0层的平均值是 3, 第1层是 14.5, 第2层是 11. 因此返回 [3, 14.5, 11].注意: 节点值的范围在32位有符号整数范围内. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *le

力扣——二叉树的层次遍历

给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如:给定二叉树 [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 返回其自底向上的层次遍历为: [ [15,7], [9,20], [3] ] /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode

力扣——二叉树的剪枝

给定二叉树根结点 root ,此外树的每个结点的值要么是 0,要么是 1. 返回移除了所有不包含 1 的子树的原二叉树. ( 节点 X 的子树为 X 本身,以及所有 X 的后代.) 示例1: 输入: [1,null,0,0,1] 输出: [1,null,0,null,1] 解释: 只有红色节点满足条件“所有不包含 1 的子树”. 右图为返回的答案. 示例2: 输入: [1,0,1,0,0,0,1] 输出: [1,null,1,null,1] 示例3: 输入: [1,1,0,1,1,0,1,0]

leetcode 637 C++ 16ms 二叉树的层平均值

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ #include<queue> class Solution { public: vector<double> averageOfLevels

leetcode 637 python3 76ms 二叉树的层平均值

# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None from collections import deque class Solution: def averageOfLevels(self, root): """ :type root: TreeNode :r

JS数据结构第六篇 --- 二叉树力扣练习题

1.第226题:翻转二叉树 递归+迭代两种实现方式: /** 反转二叉树 * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {TreeNode} * 第一种方式迭代 * 执行用时 :72 ms, 在所有 JavaScript 提

[力扣]144_二叉树的前序遍历

/* 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. */ 方法一:常规递归方式,用C语言实现(根左右) 代码实现: /** * Note

【算法练习题】力扣练习题——数组(2):三数之和

原题说明: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为:[[-1, 0, 1],[-1, -1, 2]] 原题链接:https://leetcode-cn.com/problems/3sum 解法一:基于HashMap的暴力求解 参考力扣题