637. Average of Levels in Binary Tree # 题目# Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is ...
* int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */classSolution{publicList<Double>averageOfLevels(TreeNode root){ List<Double> list =newArrayList<Double>();if(root == null) {returnlist; } Queue<TreeNode> queue =newLinkedList<TreeNode>(); queu...
1#Definition for a binary tree node.2#class TreeNode(object):3#def __init__(self, x):4#self.val = x5#self.left = None6#self.right = None78classSolution(object):9defaverageOfLevels(self, root):10"""11:type root: TreeNode12:rtype: List[float]13"""14levels = [[root]]#将要...
* Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ classSolution{ public: longlonga[100000]; intb[100000]; voidf(TreeNode*root,intd) { if(root!=NULL...
LeetCode 637. Average of Levels in Binary Tree(层序遍历)Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: <b>Input:</b> 3 / 9 20 / 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on ...
Note: The range of node's value is in the range of 32-bit signed integer. 2.解决思路和代码 循环每层的节点,保存到二维数组,二维数组的一个值保存这一层节点的值之和,另外一个值保存这一层的节点个数。 class Solution: def averageOfLevels(self, root): info = [] self.dfs(info, root, 0...
(来源:https://leetcode.com/problems/average-of-levels-in-binary-tree/) 本质上就是个遍历啊 非递归方法里,null永远不要存进stackorqueue 递归 classNodeSum{doublesum;intnum;}classSolution{publicList<Double>averageOfLevels(TreeNoderoot){List<Double>avgList=newArrayList<>();if(root==null){returnavg...
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. 给定一非空二叉树,返回其每层的平均值所构成的数组。 Example 1: Input: 3 / \ 9 20 / \ 15 7 Output:[3, 14.5, 11] ...
637. 二叉树的层平均值 - 给定一个非空二叉树的根节点 root , 以数组的形式返回每一层节点的平均值。与实际答案相差 10-5 以内的答案可以被接受。 示例 1: [https://assets.leetcode.com/uploads/2021/03/09/avg1-tree.jpg] 输入:root = [3,9,20,null,null,15,7]
637. 二叉树的层平均值 - 给定一个非空二叉树的根节点 root , 以数组的形式返回每一层节点的平均值。与实际答案相差 10-5 以内的答案可以被接受。 示例 1: [https://assets.leetcode.com/uploads/2021/03/09/avg1-tree.jpg] 输入:root = [3,9,20,null,null,15,7]