515. Find Largest Value in Each
515. Find Largest Value in Each Tree Row

515. Find Largest Value in Each Tree Row
# 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]
"""
if not root:
return []
qu = [root]
res = []
while qu:
mx = float("-inf")
for i in range(len(qu)):
node = qu.pop(0)
mx = max(mx, node.val)
if node.left:
qu.append(node.left)
if node.right:
qu.append(node.right)
res.append(mx)
return res
本文标题:515. Find Largest Value in Each
本文链接:https://www.haomeiwen.com/subject/vtnldctx.html
网友评论