637: 二叉树的层平均值
给定一个非空二叉树,返回一个由每层节点平均值组成的数组;
 
class Solution(object):
def averageOfLevels(self, root):
"""
:type root: TreeNode
:rtype: List[float]
实现方法:
设置两个数组,一个用于记录每层树的总和,一颗树记录每层树的节点个数;
"""
sum=[0.0 for i in range(1000)]
count=[0.0 for i in range(10000)] num=0
def all(root,num):
if root:
sum[num]=sum[num]+root.val
count[num]=1+count[num]
all(root.left,num+1)
all(root.right,num+1) all(root,num) res=[] for i in range(len(sum)):
if count[i]!=0:
res.append(sum[i]/count[i]) return res
注意上述的策略:
nums=[0.0 for i in range(100)]-------------[0.0,0.0........]
nums[i for i in range(100)]-----------------[0,1,2,3,4........99]
 
 
 
新定义一个函数,添加一个层数参数,如果目前答案的列表答案个数等于层数,那么当前层数的列表append新元素即可。
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
result=[] def all(root,level):
if root:
if len(result)<level+1:
result.append([])
result[level].append(root.val)
all(root.left,level+1)
all(root.right,level+1) all(root,0)
return result

Leetcode 树(102, 637)的更多相关文章

  1. LeetCode树专题

    LeetCode树专题 98. 验证二叉搜索树 二叉搜索树,每个结点的值都有一个范围 /** * Definition for a binary tree node. * struct TreeNod ...

  2. leetcode 树类型题

    树的测试框架: // leetcodeTree.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...

  3. leetcode: 树

    1. sum-root-to-leaf-numbers Given a binary tree containing digits from0-9only, each root-to-leaf pat ...

  4. [leetcode] 树 -Ⅰ

    均为 Simple 难度的水题. 二叉树的中序遍历 题目[94]:给定一个二叉树,返回它的中序 遍历. 解题思路:Too simple. class Solution { public: vector ...

  5. [leetcode] 树(Ⅱ)

    All questions are simple level. Construct String from Binary Tree Question[606]:You need to construc ...

  6. 【LeetCode】102 - Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  7. 【一天一道LeetCode】#102. Binary Tree Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  8. leetcode树专题894.897,919,951

    满二叉树是一类二叉树,其中每个结点恰好有 0 或 2 个子结点. 返回包含 N 个结点的所有可能满二叉树的列表. 答案的每个元素都是一个可能树的根结点. 答案中每个树的每个结点都必须有 node.va ...

  9. leetcode 树的锯齿形状遍历

    二叉树的锯齿形层次遍历     给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如:给定二叉树 [3,9,20,null,n ...

随机推荐

  1. C++标准库algorithm

    (1) 基本数学相关: max(t1, t2)和min(t1, t2), 返回t1和t2中的较大.较小者. max_element(b, e)和min_element(b, e), 返回两个迭代器所指 ...

  2. Linux ,Ubuntu 分区建议大小

    分区 分区类型 文件系统 大小 /boot 逻辑分区 Ext4 300M swap 逻辑分区 交换空间 13G / 主分区 Ext4 30G /home 逻辑分区 Ext4 42G /usr 逻辑分区 ...

  3. 如何使用mybatis插入数据之前就具生成id值

    SelectKey在Mybatis中是为了解决Insert数据时不支持主键自动生成的问题,该功能可以很随意的设置生成主键的方式. 不管SelectKey有多好,尽量不要遇到这种情况吧,毕竟很麻烦. k ...

  4. 为Vue.js添加友好日志

    const isDebugEnabled = process.env.NODE_ENV !== "production"; const isInfoEnabled = true; ...

  5. nginx的proxy_redirect

    proxy_redirect 语法:proxy_redirect [ default|off|redirect replacement ]; 默认:proxy_redirect default; 配置 ...

  6. VSCode打开多个项目文件夹的解决方法

    最近从sublime转vscode,自然而然就会把sublime的一些习惯带过来,其中有一点让人头疼的是: 当把一个文件夹拖进vscode里面的时候,会把原来的文件夹覆盖掉,这就意味着不能同时在vsc ...

  7. Linux使用pam_tally2.so模块限制登录失败锁定时间

    关于PAM Linux-PAM (Pluggable Authentication Modules for Linux)可插拔认证模块. https://www.cnblogs.com/klb561/ ...

  8. oracle更具uuid排序后进行分页

    oracle查询分页.一个demo,可以借用. select a.unid from ( select t.unid,rownum rowno from DEV_REG_CFG_CAMERA t wh ...

  9. mysql主从服务搭建

    一.安装mysql 检测当前centos是否安装了mysql:yum list installed | grep mysql yum list installed | grep mariadb    ...

  10. MySQL 基础 查询

    别名 查询数据时,如果表名很长,使用起来不方便,此时,就可以为表取一个别名,用这个别名来代替表的名称 .同时为了更好的显示所查询出来的字段,也可以给字段取别名. 一,表作为别名: mysql> ...