637. Average of Levels in Binary Tree(Tree)

原创 2018年04月15日 23:35:41

https://leetcode.com/problems/average-of-levels-in-binary-tree/description/

题目:求每一层元素的平均值

思路:BFS遍历

class Solution {
public:
    vector<double> averageOfLevels(TreeNode* root) {
          vector<double>result;
          if(!root) return result;

          TreeNode *q[10000];
          int num=1,num_1=1,next_num=0,l=0,r=1;

          q[l]=root;
          double sum=0;
          while(l<r){
               TreeNode *temp = q[l];
               num--;
               sum+=temp->val;
               if(temp->left) {
                  q[r]=temp->left;
                  r++;
                  next_num++;
               }
               if(temp->right) {
                  q[r]=temp->right;
                  r++;
                  next_num++;
               }
               if(num==0){
                  result.push_back(sum/num_1);
                  num=num_1=next_num;
                  next_num=0;
                  sum=0;
               }
              l++;
          }
        return result;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tangyuanzong/article/details/79955137

python--leetcode637. 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...
  • wenqiwenqi123
  • wenqiwenqi123
  • 2017-10-05 17:51:03
  • 503

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...
  • huanghanqian
  • huanghanqian
  • 2017-07-17 15:51:36
  • 1311

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...
  • jianingkid
  • jianingkid
  • 2017-07-10 18:37:05
  • 1305

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...
  • qq_26658823
  • qq_26658823
  • 2017-07-21 19:05:21
  • 840

[LeetCode] Binary Tree Postorder题解

Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes’ values. ...
  • fong_613
  • fong_613
  • 2017-09-10 15:21:07
  • 104

leetcode 637. Average of Levels in Binary Tree(java easy)

题目: Given a non-empty binary tree, return the average value of the nodes on each level in the form o...
  • Ellenfeifei
  • Ellenfeifei
  • 2017-09-16 23:14:24
  • 79

[LeetCode]637. Average of Levels in Binary Tree

[LeetCode]637. Average of Levels in Binary Tree题目描述思路实际上是树的层次遍历 递归写法参数中加入层数即可代码#include #include u...
  • Lcharon
  • Lcharon
  • 2017-07-17 00:52:23
  • 569

【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...
  • lx583274568
  • lx583274568
  • 2017-07-31 16:30:16
  • 190

python 实现的 binary tree

使用python 实现的 binary tree,代码如下: #!/usr/bin/python class btree: def __init__(self,value): self.le...
  • likun_tech
  • likun_tech
  • 2012-02-18 21:25:09
  • 3120

【LeetCode】637. Average of Levels in Binary Tree 解题报告

【LeetCode】637. Average of Levels in Binary Tree 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.co...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2018-01-17 18:22:32
  • 21
收藏助手
不良信息举报
您举报文章:637. Average of Levels in Binary Tree(Tree)
举报原因:
原因补充:

(最多只允许输入30个字)