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 class Solution(object):
def largestValues(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
res = []
if root == None:
return res
queue = []
root.index = 0 #修改TreeNode定义,加入index成员,表示该节点所在的层数
res.append(root.val)
p = root
while p!= None:
#print p.val
if p.left != None:
p.left.index = p.index + 1
queue.append(p.left)
if p.right != None:
p.right.index = p.index + 1
queue.append(p.right)
if len(res) <= p.index:
res.append(p.val)
else:
res[p.index] = max(res[p.index],p.val) #比较每层的最大值
if len(queue) == 0:
break
p = queue[0]
del queue[0]
return res

【leetcode】Find Largest Value in Each Tree Row的更多相关文章

  1. 【LeetCode】764. Largest Plus Sign 解题报告(Python)

    [LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  2. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  3. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  4. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

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

  6. 【LeetCode】1007. Minimum Domino Rotations For Equal Row 解题报告(Python)

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

  7. 【Leetcode】179. Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  8. 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)

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

  9. 【LeetCode】952. Largest Component Size by Common Factor 解题报告(Python & C++)

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

随机推荐

  1. 【ABAP系列】SAP ABAP7.40新语法简介第二篇

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP7.40新语法简 ...

  2. Python学习之协程

    8.8 协程 ​ 我们都知道线程间的任务切换是由操作系统来控制的,而协程的出现,就是为了减少操作系统的开销,由协程来自己控制任务的切换 ​ 协程本质上就是线程.既然能够切换任务,所以线程有两个最基本的 ...

  3. SpringCloud解决了哪些问题?

    1.与分布式系统相关的复杂性 – 包括网络问题,延迟开销,带宽问题,安全问题. 2.处理服务发现的能力 – 服务发现允许集群中的进程和服务找到彼此并进行通信. 3.解决冗余问题 – 冗余问题经常发生在 ...

  4. 安装 docker-registry-frontend

    拉取镜像  最新的V2 docker pull konradkleine/docker-registry-frontend:v2 创建 docker-compose.yml version: ' se ...

  5. CentOS 7 Tomcat 启动后 外部无法访问的问题

    1.启动tomcat 2.   验证tomcat 是否启动成功 ps -ef|grep tomcat  这样是启动成功了的 3 检查防火墙是否启动 firewall-cmd --state 防火墙 已 ...

  6. Metinfo5.1 /member/getpassword.php SQL注入

  7. 使用Spring Initializr初始化SpringBoot项目

    虽然SpringBoot CLI消除了不少设置工作,但如果你更倾向于传统的Java项目结构,那你应该看看Spring Initializr. Spring Initializr从本质上来说就是一个we ...

  8. show slave status参数详解

    root@localhost (none)>show slave status\G *************************** 1. row ******************** ...

  9. vultr vps 开启BBR加速 (CentOS 7)

    上个月买的vultr的vps 感觉看视频还是比较慢的 于是上网找教程开启BBR加速 在这里记录一下 以后可能会用到 BBR 是 Google 提出的一种新型拥塞控制算法,可以使 Linux 服务器显著 ...

  10. ucloud启用redis

    可以设置密码