Average of Levels in Binary Tree
来源:互联网 发布:wingware python ide 编辑:程序博客网 时间:2024/06/14 03:26
题意:求每一层的数据平均值,放到Vector里
思路:层序遍历,没啥难的
/** * 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<double> v; queue<TreeNode*> q; q.push(root); while(!q.empty()){ int num = q.size(); double sum = 0; for(int i = 0;i<num ;i++){ TreeNode* temp = q.front(); q.pop(); sum += temp->val; if(temp->left!=NULL){ q.push(temp->left); } if(temp->right!=NULL){ q.push(temp->right); } } v.push_back(sum/num); } return v; }};
阅读全文
0 0
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 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
- LeetCode 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
- Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- Average of Levels in Binary Tree(leetcode)
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 仿百度分页
- jQuery Ajax 返回值
- CXF 拦截器预处理request、response消息
- SecureCRT远程连接虚拟机CentOS的三种方式
- ShopStore项目总结(一)
- Average of Levels in Binary Tree
- C语言——\r\n 和 \n的区别
- 程序员之思考
- 手机验证
- angularjs简单购物车源码
- POJ2774Long Long Message
- C语言中的操作符总结
- 《自私的基因》
- HDU题目分类(转载)