↓Skip to main content
  1. LeetCode/

LeetCode 515: Find Largest Value in Each Tree Row

·1 min· ·
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-10-24

  • Submissions: Accepted (first try)

Approach
#

BFS level order (Tree Template 2), keeping one maximum per level. Values can be negative (down to -2^31), so each level starts from -inf.

My Attempts
#

Accepted on the first try.

Solution
#

class Solution:
    def largestValues(self, root: Optional[TreeNode]) -> List[int]:
        if not root:
            return []
        res = []
        queue = deque([root])
        while queue:
            best = -inf
            for _ in range(len(queue)):
                node = queue.popleft()
                best = max(best, node.val)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            res.append(best)
        return res

Complexity
#

Time O(n), space O(w).

Mistakes to Avoid
#

  • Starting each level’s maximum at 0 is wrong when a whole level is negative.
  • Return [] for an empty tree.

Interview Explanation
#

I go level by level with BFS and take the maximum of each level starting from -inf, since values can be negative. Time O(n), space is the widest level. A DFS with depth works too: res[depth] = max(res[depth], val).

Related

LeetCode 314: Binary Tree Vertical Order Traversal
·2 mins
LeetCode Medium Hash-Table Tree Depth-First-Search Breadth-First-Search Sorting Binary-Tree
LeetCode 314 note: BFS with a column index; BFS is what guarantees top-to-bottom within a column and left-to-right within a row
LeetCode 117: Populating Next Right Pointers in Each Node II
·2 mins
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 117 note: connect next pointers level by level with BFS; for a non-perfect tree, looking only at the neighbour’s children breaks the chain
LeetCode 1448: Count Good Nodes in Binary Tree
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 1448 note: pass the path maximum down during DFS; a node is good when its value is at least that maximum
LeetCode 199: Binary Tree Right Side View
·2 mins
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 199 note: in a level-order traversal, each level’s rightmost node is what you see from the right
LeetCode 1372: Longest ZigZag Path in a Binary Tree
·2 mins
LeetCode Medium Dynamic-Programming Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 1372 note: each node returns two values, the longest zigzag starting left and starting right; going left extends the left child’s start-right value
LeetCode 687: Longest Univalue Path
·2 mins
LeetCode Medium Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 687 note: DFS returns the longest same-value downward chain in edges, and each node updates the answer with left + right