LeetCode637. 二叉树的层平均值
题目
1 class Solution {
2 public:
3 vector<double>ans;
4 vector<double> averageOfLevels(TreeNode* root) {
5 if(!root) return ans;
6 queue<TreeNode*>q;
7 q.push(root);
8 while(!q.empty()){
9 int num = q.size();double sum = 0;
10 for(int i = 0;i < num;i++){
11 TreeNode* node = q.front();q.pop();
12 sum += node->val;
13 if(node->left!=NULL) q.push(node->left);
14 if(node->right!=NULL) q.push(node->right);
15 }
16 ans.push_back(sum/num);
17 }
18 return ans;
19 }
20 };
LeetCode637. 二叉树的层平均值的更多相关文章
- [Swift]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 ...
- LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)
637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目 ...
- LeetCode 637. Average of Levels in Binary Tree二叉树的层平均值 (C++)
题目: Given a non-empty binary tree, return the average value of the nodes on each level in the form o ...
- Java实现 LeetCode 637 二叉树的层平均值(遍历树)
637. 二叉树的层平均值 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3 / \ 9 20 / \ 15 7 输出: [3, 14.5, 11] 解释: 第0层的 ...
- Leetcode:637. 二叉树的层平均值
Leetcode:637. 二叉树的层平均值 Leetcode:637. 二叉树的层平均值 Talk is cheap . Show me the code . /** * Definition fo ...
- [LeetCode] 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 ...
- Leetcode637.Average of Levels in Binary Tree二叉树的层平均值
给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. class Solution { public: vector<double> averageOfLevels(TreeNode ...
- [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 ...
- 《程序员代码面试指南》第三章 二叉树问题 二叉树按层打印和ZigZag打印
题目 二叉树按层打印和ZigZag打印 java代码 package com.lizhouwei.chapter3; import java.util.LinkedList; import java. ...
随机推荐
- oracle DG搭建
Oracle DG 搭建1. 环境 OS IP hostname db_name DB_UNIQUE_NAME主库 RHEL 5.4 192.168.12.20 edgzrip1.oracle.com ...
- 附028.Kubernetes_v1.20.0高可用部署架构二
目录 kubeadm介绍 kubeadm概述 kubeadm功能 本方案描述 部署规划 节点规划 初始准备 互信配置 其他准备 集群部署 相关组件包 正式安装 部署高可用组件I Keepalived安 ...
- openstack高可用集群20-openstack计算节点宕机迁移方案
openstack计算节点宕机迁移方案 情景一:/var/lib/nova/instances/ 目录不共享的处理方法(类似手动迁移云主机到其他节点)
- 基于Redis的消息队列使用:spring boot2.0整合redis
一 . 引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...
- java中使用IO流将以文件中的内容去取到指定的文件中
public class Demo12 { public static void main(String[] args) throws IOException { File file=new File ...
- mysql中sql行列转换
1.列转行 select class_id,MAX(CASE kemu when '语文' then score ELSE 0 end)as '语文' ,MAX(CASE kemu when '数学' ...
- Oracle中除数为0的两种解决办法(decode与nullif)
Oracle中Decode函数,语句DECODE(tag,''ZCGS'',0,1)=decode(''@corp-No@'',''6010'',1,0) decode(字段或字段的运算,值1,值2, ...
- Java Tree 树 数据结构
说到树结构就不得不回顾 链表结构 https://www.cnblogs.com/easyidea/p/13371863.html 如果链表结构中再多一个指针会是什么情况? 是不是像树根一样,这就是 ...
- 使用JWT+RSA完成SSO单点登录
无状态登录原理 1.1.什么是有状态? 有状态服务,即服务端需要记录每次会话的客户端信息,从而识别客户端身份,根据用户身份进行请求的处理,典型的设计如tomcat中的session. 例如登录:用户登 ...
- TurtleBot3 Waffle (tx2版华夫)(5)激活你的雷达
重要提示:请在配网通信成功后进行操作,配网后再次开机需要重新验证通信: 重要提示:[Remote PC]代表PC端.[TurtelBot]代表树莓派端: 5.1.操作步骤 1)[Remote PC] ...