'''
You need to find the largest value in each row of a binary tree. Example:
Input: 1
/ \
3 2
/ \ \
5 3 9 Output: [1, 3, 9] ''' # Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None 这道题描述的是:
  给我们一颗二叉树的根节点,返回每一层最大值的数组 思想:
  广度优先遍历,把每一层最大值放入结果列表 python实现:
 class Solution(object):
def largestValues(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if not root:
return []
levels = [[root]]
values = [[root.val]]
for level in levels:
next_level = []
value = []
for node in level:
if node.left is not None:
next_level.append(node.left)
value.append(node.left.val)
if node.right is not None:
next_level.append(node.right)
value.append(node.right.val)
if next_level:
levels.append(next_level)
values.append(value)
res = []
for v in values:
res.append(max(v))
return res
精简一下代码:
 class Solution(object):
def largestValues(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
values = []
level = [root]
while any(level):
values.append( max( [i.val for i in level] ) )
level = [child for node in level for child in [node.left, node.right ] if child is not None]
return values
												

leetcode算法: Find Largest Value in Each Tree Row的更多相关文章

  1. LN : leetcode 515 Find Largest Value in Each Tree Row

    lc 515 Find Largest Value in Each Tree Row 515 Find Largest Value in Each Tree Row You need to find ...

  2. (BFS 二叉树) leetcode 515. Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  3. 【leetcode】Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  4. Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...

  5. LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    515. 在每个树行中找最大值 515. Find Largest Value in Each Tree Row 题目描述 You need to find the largest value in ...

  6. LeetCode算法题-Trim a Binary Search Tree(Java实现)

    这是悦乐书的第284次更新,第301篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第152题(顺位题号是669).给定二叉搜索树以及L和R的最低和最高边界,修剪树以使其所 ...

  7. LeetCode算法题-Construct String from Binary Tree(Java实现)

    这是悦乐书的第273次更新,第288篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第141题(顺位题号是606).构造一个字符串,该字符串由二叉树中的括号和整数组成,并具 ...

  8. LeetCode算法题-Largest Number At Least Twice of Others(Java实现)

    这是悦乐书的第308次更新,第328篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第177题(顺位题号是747).在给定的整数数组中,总有一个最大的元素.查找数组中的最大 ...

  9. LeetCode算法题-Maximum Depth of N-ary Tree(Java实现)

    这是悦乐书的第261次更新,第274篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第128题(顺位题号是559).给定n-ary树,找到它的最大深度.最大深度是从根节点到 ...

  10. LeetCode算法题-Convert BST to Greater Tree(Java实现)

    这是悦乐书的第255次更新,第268篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第122题(顺位题号是538).给定二进制搜索树(BST),将其转换为更大树,使原始BS ...

随机推荐

  1. springMVC正确使用GET POST PUT和DELETE方法,如何传递参数

    1. 向服务器请求数据:GET 这是标准的http的GET最擅长的, 应该使用GET请求,但是在使用时候我们会需要传递一个或多个参数给服务器, 这些出参数可能是基本数据类型页可能是对象,get方法可以 ...

  2. js中==和===区别

    简单来说: == 代表相同, ===代表严格相同, 为啥这么说呢, 这么理解: 当进行双等号比较时候: 先检查两个操作数数据类型,如果相同, 则进行===比较, 如果不同, 则愿意为你进行一次类型转换 ...

  3. Spring - JPA 一对一, 一对多, 多对多关联

    现在有三个类:One Many Much One类 Much类 @Entity public class Much { @Id @GeneratedValue private Integer id; ...

  4. js对象系列【一】深层理解对象与原型

    我们先从盘古开天辟地时捋一捋对象: 从宏观内容来讲,javascript是一个属性的集合,包括值,函数,而整个集合也可以类比为一个对象. js = { a的变量名: a的值, ... 函数b: fun ...

  5. JS面向对象与面向过程

    前言 面向对象编程: 就是将你的需求抽象成一个对象,然后针对这个对象分析其特征(属性)与动作(方法)--这个对象就称之为类 面向过程编程: 特点:封装,就是将你需要的功能放在一个对象里面 ------ ...

  6. 用golang 实现一个代理池

    背景 写爬虫的时候总会遇到爬取速度过快而被封IP的情况,这个时候就需要使用代理了.在https://github.com/henson/ProxyPool 的启发下,决定自己实现一个代理池.项目已经开 ...

  7. 笔记:创建Jersey REST 服务,基于Maven

    基于Java SE 形式的REST服务 创建项目 我们首选使用 archetypeGroupId 为 org.glassfish.jersey.archetypes 的原型,archetypeArti ...

  8. Redis登录密码设置

    1. 更改Redis.conf配置 # requirepass foobared 去掉注释,foobared改为 自己的password , 我测试的时候用的是默认的 foobared 2.启动red ...

  9. Algorithm --> 字符串中最长不重合子串长度

    例子 "abmadsefadd"  最长长度为7 "avoaid"           最长长度为3 思路 空间换时间hashTable,起始位置设为beg.初 ...

  10. java开源安全框架-------Apache Shiro--第一天

    1.1.简介 Apache Shiro 是Java的一个安全框架.目前使用Apache Shiro 的人越来越多,因为它相当简单,对比Spring Security,可能没有没有Spring Secu ...