leetCode题解之求二叉树每层的平均值

1、题目描述

Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.

计算二叉树每一层的节点的数据域的平均值。

2、题目分析

使用广度优先遍历方法,逐层对二叉树进行访问,求均值。使用一个队列数据结构完成对二叉树的访问,队列(queue)的特点是FIFO,即先进先出,queue的几种常用的方法是:

queue::front()  :访问队首元素

queue:: pop()  删除队首元素

queue::push()  入队

queue:: back()  访问队尾元素

 1 vector<double> averageOfLevels(TreeNode* root) {
 2
 3         vector<double> ave;
 4         queue<TreeNode*> q;
 5         q.push(root);
 6
 7         while(!q.empty())
 8             {
 9                 double temp = 0.0;
10                 int s = q.size();
11                 for( int i = 0; i < s;i++ )
12                     {
13                         temp += q.front()->val;
14                         if(q.front()->left) q.push(q.front()->left);
15                         if(q.front()->right ) q.push(q.front()->right);
16                         q.pop();
17                     }
18                 ave.push_back(temp/s);
19
20             }
21         return ave;
22
23
24
25     }

3、代码

原文地址:https://www.cnblogs.com/wangxiaoyong/p/8686699.html

时间: 2024-08-29 23:13:32

leetCode题解之求二叉树每层的平均值的相关文章

leetCode题解之反转二叉树

1.题目描述 经典的反转二叉树,就是将二叉树中每个节点的左.右儿子交换. 2.题目分析 3.代码 1 TreeNode* invertTree(TreeNode* root) { 2 3 if(root == NULL ) 4 return NULL; 5 6 7 TreeNode* temp; 8 temp = root->left; 9 root->left = invertTree(root->right); 10 root->right = invertTree(temp)

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

Leetcode题解 - 双指针求n数之和

1. 两数之和 """ 双指针,题目需要返回下标,所以记录一个数字对应的下标 """ class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: r = [[ind, num] for ind, num in enumerate(nums)] r = sorted(r, key=lambda x: x[1]) head = 0 tail = len

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order tr

leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] co

求二叉树第K层的节点个数+求二叉树叶子节点的个数

size_t _FindLeafSize(Node* root)     //求二叉树叶子节点的个数    {        //static size_t count = 0;        if (root == NULL)            return 0; if (root->_left == NULL&&root->_right == NULL);        return 1; return _FindLeafSize(root->_right) +

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(