"""
BFS遍历题,一遍AC
Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
Return the smallest level X such that the sum of all the values of nodes at level X is maximal.
Example 1:
Input: [1,7,0,7,-8,null,null]
Output: 2
Explanation:
Level 1 sum = 1.
Level 2 sum = 7 + 0 = 7.
Level 3 sum = 7 + -8 = -1.
So we return the level with the maximum sum which is level 2.
"""
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution:
def maxLevelSum(self, root):
if root == None:
return 0
queue = []
res = []
queue.append(root)
while queue:
n = len(queue)
sum = 0
newqueue = []
for _ in range(n):
x = queue.pop(0)
sum += x.val
if x.left != None:
newqueue.append(x.left)
if x.right != None:
newqueue.append(x.right)
queue = newqueue
res.append(sum)
return res.index(max(res))+1

leetcode1161 Maximum Level Sum of a Binary Tree的更多相关文章

  1. 【leetcode】1161. Maximum Level Sum of a Binary Tree

    题目如下: Given the root of a binary tree, the level of its root is 1, the level of its children is 2, a ...

  2. 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  3. [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  4. 102. Binary Tree Level Order Traversal + 103. Binary Tree Zigzag Level Order Traversal + 107. Binary Tree Level Order Traversal II + 637. Average of Levels in Binary Tree

    ▶ 有关将一棵二叉树转化为二位表的题目,一模一样的套路出了四道题 ▶ 第 102 题,简单的转化,[ 3, 9, 20, null, null, 15, 7 ] 转为 [ [ 15, 7 ] , [ ...

  5. 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 / \

    class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vect ...

  6. PAT (Advanced Level) 1102. Invert a Binary Tree (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  7. [leetcode]Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  8. 【leetcode】Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  9. 26. Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

随机推荐

  1. requests库GET

    文档地址:http://docs.python-requests.org/zh_CN/latest/index.html import requests res = requests.get('htt ...

  2. 3 Struts2的常见配置解析

    1 package标签的相关配置 package标签:包,与Java中的包概念不一致.旨在更好的管理actionpackage标签的属性: name :  包的名称,在一个项目不重名即可,无具体含义 ...

  3. Java--对象与类(二)

    final 实例域 可以将实例域定义为final.构建对象时必须初始化这样的域.也就是说在一个构造器执行之后,这个域被设置,并且之后无法对其修改 final 修饰符大多应用于基本(primitive) ...

  4. px(像素)、pt(点)、ppi、dpi、dp、sp之间的关系

    px:pixel,像素,电子屏幕上组成一幅图画或照片的最基本单元 pt:point,点,印刷行业常用单位,等于1/72英寸 ppi:pixel per inch,每英寸像素数,该值越高,则屏幕越细腻 ...

  5. ‘A’ = N’A’ , will not kill index seek in sqlserver version above 2014 anymore

    Thanks to The advisor show us that we can get different behavior when we use condition , ='20000' or ...

  6. 通过Java创建XML(中文乱码已解决)

    package com.zyb.xml; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.Ou ...

  7. Spring @Async之一:实现异步调用示例

    什么是“异步调用”? “异步调用”对应的是“同步调用”,同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执行:异步调用指程序在顺序执行时,不等待异步调用的语句返回结果 ...

  8. FTP的vsftpd.conf含义

    # 设置为YES时vsftpd以独立运行方式启动,设置为NO时以xinetd方式启动 #(xinetd是管理守护进程的,将服务集中管理,可以减少大量服务的资源消耗) listen=YES # 同上,如 ...

  9. ASP.NET MVC4 Web项目中使用Log4Net记录日志到文件和数据库。

    下载与.netframework版本向对应的log4net.dll ,然后添加引用.下载地址:http://logging.apache.org/log4net/download_log4net.cg ...

  10. 绕过waf

    WAF:有硬件和软件类型. 常见的软WAF,常见:安全狗.云锁.云盾.护卫神. SQL注入的绕过:  WAF核心机制就是正则匹配. 通过正则匹配,如果符合规则,就拦截. 比如sql注入中and 1=1 ...