> For the complete documentation index, see [llms.txt](https://cai-sen-se.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cai-sen-se.gitbook.io/leetcode/401-600/515.-zai-mei-ge-shu-hang-zhong-zhao-zui-da-zhi.md).

# 515. 在每个树行中找最大值

<https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/>

## 解法一：层序

记录每层最大值

```python
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
import queue
class Solution:
    def largestValues(self, root: TreeNode) -> List[int]:
        res = []
        if not root:
            return res
        q = queue.Queue()
        q.put(root)
        while not q.empty():
            count = q.qsize()
            maxVal = -sys.maxsize
            for _ in range(count):
                node = q.get()
                maxVal = max(maxVal, node.val)    #更新最大值
                if node.left:
                    q.put(node.left)
                if node.right:
                    q.put(node.right)
            res.append(maxVal)
        return res
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cai-sen-se.gitbook.io/leetcode/401-600/515.-zai-mei-ge-shu-hang-zhong-zhao-zui-da-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
