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. 分享我积攒的测试相关的资料收集awesome-test

    微信扫描关注我的公众号,回复测试资料 即可免费获取资料下载地址,不定期更新资料

  2. 高级测试工程师面试必问面试基础整理——python基础(一)(首发公众号:子安之路)

    现在深圳市场行情,高级测试工程师因为都需要对编程语言有较高的要求,但是大部分又没有python笔试机试题,所以面试必问python基础,这里我整理一下python基本概念,陆续收集到面试中python ...

  3. 关于Qt中的QMutex——关于lock与unlock

    大概说一下用法,例子: QMutex mutex; int number = 6; void method1() { mutex.lock(); number *= 5; number /= 4; m ...

  4. Angular5 tslint错误:The selector of the component “XXXComponent” should be used as element

    错误描述 在项目中自己封装了一个 select 组件 @Component({ selector: '[app-choosen-select]', templateUrl: './selectcomm ...

  5. mysql——单表查询——聚合函数——示例

    ), km ), cj ) ); select * from score; ,); ,); ,); ,); ,); ,); ,); ,); ,); ,); ,); ,); ; 查询此同学的总成绩: ; ...

  6. [转帖].NET Core单文件发布静态编译AOT CoreRT

    .NET Core单文件发布静态编译AOT CoreRT https://www.cnblogs.com/linezero/p/CoreRT.htm .NET Core单文件发布静态编译AOT Cor ...

  7. mysql应用之通过存储过程方式批量插入数据

    我们平时的测试过程中有一个环节就是准备测试数据,包括准备基础数据,准备业务数据,使用的场景包括压力测试,后台批量数据传输,前端大数据查询导出,或者分页打印等功能,准备测试数据我们通俗点讲就是造数据,根 ...

  8. Spark架构角色及基本运行流程

    1. 集群角色 Application:基于spark的用户程序,包含了一个Driver program 和集群中多个Executor Driver Program:运行application的mai ...

  9. A Dangerous Maze (期望值)

    https://vjudge.net/problem/LightOJ-1027?tdsourcetag=s_pctim_aiomsg [被满为姐姐碾压 降智打击题]

  10. CentOS7下载与安装错误全记录

    这篇文章记录安装CentOS7过程错误全记录,供大家和自己参考 起因:笔记本用的win10系统,开启热点的时候,总是10分钟就自动关闭.于是折腾linux系统,平时用win10系统,也切换到linux ...