Leetcode 树(102, 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
- 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)的更多相关文章
- LeetCode树专题
LeetCode树专题 98. 验证二叉搜索树 二叉搜索树,每个结点的值都有一个范围 /** * Definition for a binary tree node. * struct TreeNod ...
- leetcode 树类型题
树的测试框架: // leetcodeTree.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- leetcode: 树
1. sum-root-to-leaf-numbers Given a binary tree containing digits from0-9only, each root-to-leaf pat ...
- [leetcode] 树 -Ⅰ
均为 Simple 难度的水题. 二叉树的中序遍历 题目[94]:给定一个二叉树,返回它的中序 遍历. 解题思路:Too simple. class Solution { public: vector ...
- [leetcode] 树(Ⅱ)
All questions are simple level. Construct String from Binary Tree Question[606]:You need to construc ...
- 【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, ...
- 【一天一道LeetCode】#102. Binary Tree Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- leetcode树专题894.897,919,951
满二叉树是一类二叉树,其中每个结点恰好有 0 或 2 个子结点. 返回包含 N 个结点的所有可能满二叉树的列表. 答案的每个元素都是一个可能树的根结点. 答案中每个树的每个结点都必须有 node.va ...
- leetcode 树的锯齿形状遍历
二叉树的锯齿形层次遍历 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如:给定二叉树 [3,9,20,null,n ...
随机推荐
- P2678 跳石头---(二分答案)
题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 NNN 块岩石 ...
- ms17_010_psexec
一.ms17_010_psexec简介 MS17-010 的psexec是针对Microsoft Windows的两款最受欢迎的漏洞进行攻击. CVE-2017-0146(EternalChampio ...
- appium api笔记
打印上下文driver.contexts打印当前上下文driver.contextdriver.current_context切换上下文driver.switch_to.context('WEBVIE ...
- Cassandra最小化安装
Cassandra最小化安装 环境 CentOS 7.2 64位 IP_address:172.27.0.8 安装包装备 [root@master ~]# ll /usr/local/src tota ...
- mysql_主从同步
在这里我就不说怎么搭建 Mysql 数据库了!如果有需要可以参照我前面的博文. 此博文主要说配置 Linux 数据库 主从 下面我们开始进入正题. master:192.168.31.200 ...
- nodejs启动web项目
1.在根目录路径下输入 npm install ,会自动下载所需的包 2.安装完成对应的包以后,npm start,会自动打开浏览器
- Linux(CentOs 7)系统重装笔记(二)---完全删除用户账号和root用户登录
参考网址:https://jingyan.baidu.com/article/046a7b3ede1c38f9c27fa91b.html 一.完全删除用户 1.查看要删除的用户账号信息 find / ...
- rpm和yum模拟安装
在更新安装包之前,我们可能会想做一个测试运行,换句话说,模拟而不是实际安装更新的包,以确定在安装之前是否有任何需要处理的问题. 以测试更新openssh2为例: yum update openssh2 ...
- centos7的systemd命令对比
centos7的systemd命令对比 http://www.linuxidc.com/Linux/2014-09/106490p2.htmhttp://www.linuxidc.com/Linux/ ...
- dataTable使用方法
using System; using System.Data; using System.Data.SqlClient; namespace App{ class MyClass{ public s ...