637. Average of Levels in Binary Tree 二叉树每层平均值
来源:互联网 发布:l2tp拨号软件 编辑:程序博客网 时间:2024/05/29 18:25
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 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].
Note:
1. The range of node’s value is in the range of 32-bit signed integer.
思路
【方法1】深度遍历
记录每层的结点总数和结点和。
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<double> averageOfLevels(TreeNode* root) { vector<int> count; vector<double> res; average(root,0,count,res); for(int i=0;i<res.size();i++){ res[i]=res[i]/count[i]; } return res; } void average(TreeNode *root, int i, vector<int> &count, vector<double> &sum){ if(!root) return; if(i<sum.size()){ sum[i]+=root->val; count[i]++; } else{ sum.push_back(1.0*root->val); count.push_back(1); } average(root->left,i+1,count,sum); average(root->right,i+1,count,sum); }};
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<Double> averageOfLevels(TreeNode root) { List<Integer> count=new ArrayList<>(); List<Double> res=new ArrayList<>(); average(root,0,count,res); for(int i=0;i<res.size();i++){ res.set(i,res.get(i)/count.get(i)); } return res; } public void average(TreeNode root, int i, List<Integer> count, List<Double> sum){ if(root==null) return; if(i<sum.size()){ sum.set(i,sum.get(i)+root.val); count.set(i,count.get(i)+1); } else{ sum.add(1.0*root.val); count.add(1); } average(root.left,i+1,count,sum); average(root.right,i+1,count,sum); }}
阅读全文
0 0
- 637. Average of Levels in Binary Tree 二叉树每层平均值
- leetCode 637. Average of Levels in Binary Tree 求二叉树每层平均值
- leetcode Average of Levels in Binary Tree 二叉树每层的平均值
- leetcode 637. Average of Levels in Binary Tree二叉树的每层平均数
- 【二叉树】637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- DES加密和解密
- Http协议中常用字段总结(不定时完善中)
- 第二篇:JAVA获得任务栏高度
- html 后代选择器和子元素选择器
- jdk1.8+Tomcat7.0小版本无法兼容问题解决
- 637. Average of Levels in Binary Tree 二叉树每层平均值
- sftp的rm和rmdir命令
- 用MATLAB求解拟合公式系数和拟合优度
- Fragment
- javascrip执行顺序问题
- jQuery之异步Ajax请求使用 通过传递参数来调用后台方法返回状态
- 各种资源网站收集
- socket跟TCP/IP 的关系,单台服务器上的并发TCP连接数可以有多少
- 6.优化一下代码,添加一个GUI的用户操作界面