515. Find Largest Value in Each Tree Row 二叉树每一层的最大值
Posted Long Long Journey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 9 Output: [1, 3, 9]
题意:找出二叉树一层的最大值
解法:用BFS的方式遍历每一层
class Solution(object):
def largestValues(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
result = []
if not root:
return result
queue = [root]
while queue:
count = len(queue)
res = -2147483648
for i in range(count):
curNode = queue.pop(0)
res = max(curNode.val, res)
if curNode.left:
queue.append(curNode.left)
if curNode.right:
queue.append(curNode.right)
result.append(res)
return result
以上是关于515. Find Largest Value in Each Tree Row 二叉树每一层的最大值的主要内容,如果未能解决你的问题,请参考以下文章
515. Find Largest Value in Each Tree Row
515. Find Largest Value in Each Tree Row
leetcode--515. Find Largest Value in Each Tree Row
[LeetCode]515 Find Largest Value in Each Tree Row(dfs)