Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

515. Find Largest Value in Each Tree Row

Medium65.7% Acceptance
TreeDepth-First SearchBreadth-First Search
Asked by:
Amazon
ProblemSolutions (9)VideosCompanies (6)Notes

Problem Statement

Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).

Example 1:

Input: root = [1,3,2,5,3,null,9]
Output: [1,3,9]

Example 2:

Input: root = [1,2,3]
Output: [1,3]

Constraints:

  • The number of nodes in the tree will be in the range [0, 104].
  • -231 <= Node.val <= 231 - 1
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
A
Apple
L
LinkedIn
S
Samsung
F
Facebook
+1

Approach

In #515 Find Largest Value in Each Tree Row, the goal is to determine the maximum value at every depth level of a binary tree. The problem naturally maps to level-wise traversal of the tree.

A common strategy is Breadth-First Search (BFS). Using a queue, traverse the tree level by level. For each level, track the maximum value among all nodes currently in the queue before moving to the next level. This ensures that each row of the tree is processed independently.

Another effective approach is Depth-First Search (DFS). While recursively traversing the tree, keep track of the current depth. If visiting a level for the first time, store the value; otherwise, update the stored value with the maximum of the existing and current node values.

Both methods visit each node exactly once, resulting in efficient traversal with linear time complexity.

Complexity

ApproachTime ComplexitySpace Complexity
Breadth-First Search (Level Order)O(n)O(w) where w is the maximum width of the tree
Depth-First Search (Recursive)O(n)O(h) where h is the height of the tree

Video Solution Available

Nick White

View all video solutions

Solutions (9)

Breadth-First Search (BFS) Approach

The BFS approach involves traversing the tree level by level and keeping track of the largest value at each level. We employ a queue data structure to facilitate level-wise traversal and maintain a list to store the maximum values for each row.

Time Complexity: O(n) where n is the number of nodes in the tree, since each node is processed exactly once. Space Complexity: O(n) due to the storage required to keep the queue and the result array.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3#include <limits.h>
4
5// Definition for a binary tree node.
6struct TreeNode {








Explanation

This solution uses a queue to perform a BFS traversal on the binary tree. Each level is processed entirely before moving to the next, and the largest value encountered at each level is recorded in the result array. The function returns the array of largest values and updates the size of the returned array via the pointer returnSize.

Depth-First Search (DFS) Approach

The DFS approach involves recursively exploring each branch of the tree depth-first and leveraging the depth of recursion to determine the level of the tree. For each level not yet considered, the maximum value is initialized, or updated if a new larger value is found.

Time Complexity: O(n) where n is the number of nodes in the tree, since every node is visited once. Space Complexity: O(h) where h is the height of the tree due to recursion stack.

C++JavaPython
1#include <vector>
2#include <algorithm>
3#include <climits>
4
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

void dfs(TreeNode* node, int depth, vector<int>& maxValues) {
    if (!node) return;
    if (depth == maxValues.size()) {
        maxValues.push_back(node->val);
    } else {
        maxValues[depth] = max(maxValues[depth], node->val);
    }
    dfs(node->left, depth + 1, maxValues);
    dfs(node->right, depth + 1, maxValues);
}

vector<int> largestValues(TreeNode* root) {
    vector<int> maxValues;
    dfs(root, 0, maxValues);
    return maxValues;
}

Video Solutions

Watch expert explanations and walkthroughs

LeetCode Find Largest Value In Each Tree Row Explained - Java

Nick White
6:0917,048 views

Asked By Companies

6 companies
A
Amazon
A
Apple
L
LinkedIn
S
Samsung
F
Facebook
U
UnitedHealth

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Binary Tree Inorder TraversalEasy
Validate Binary Search TreeMedium
Recover Binary Search TreeMedium
Same TreeEasy
More similar problems

Related Topics

TreeDepth-First SearchBreadth-First SearchBinary Tree

Problem Stats

Acceptance Rate65.7%
DifficultyMedium
Companies6

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Find Largest Value in Each Tree Row asked in FAANG interviews?

Yes, variations of level-order traversal problems are common in FAANG and other top tech company interviews. This question tests understanding of binary tree traversal, recursion, and level-based processing.

What data structure is best for Find Largest Value in Each Tree Row?

A queue is typically used for the BFS approach to process nodes level by level. For the DFS approach, recursion along with a list or array to store the maximum value at each depth works efficiently.

What is the optimal approach for Find Largest Value in Each Tree Row?

The optimal approaches are Breadth-First Search (level-order traversal) and Depth-First Search with depth tracking. Both methods visit each node once and keep track of the maximum value at every level of the binary tree.

Can Find Largest Value in Each Tree Row be solved using DFS instead of BFS?

Yes, DFS works well by tracking the current depth during recursion. You maintain a list where each index represents a level and update the stored value with the maximum encountered at that depth.

7
int
val
;
8
struct
TreeNode
*
left
;
9
struct
TreeNode
*
right
;
10
}
;
11
12
#
define
MAX_QUEUE_SIZE
10000
13
14
struct
TreeNode
*
queue
[
MAX_QUEUE_SIZE
]
;
15
int
front
=
0
;
16
int
rear
=
0
;
17
18
void
enqueue
(
struct
TreeNode
*
node
)
{
19
if
(
rear
<
MAX_QUEUE_SIZE
)
{
20
queue
[
rear
++
]
=
node
;
21
}
22
}
23
24
struct
TreeNode
*
dequeue
(
)
{
25
return
queue
[
front
++
]
;
26
}
27
28
int
*
largestValues
(
struct
TreeNode
*
root
,
int
*
returnSize
)
{
29
if
(
!
root
)
{
30
*
returnSize
=
0
;
31
return
NULL
;
32
}
33
34
int
*
result
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
MAX_QUEUE_SIZE
)
;
35
*
returnSize
=
0
;
36
37
front
=
rear
=
0
;
38
enqueue
(
root
)
;
39
while
(
front
!=
rear
)
{
40
int
levelSize
=
rear
-
front
;
41
int
levelMax
=
INT_MIN
;
42
for
(
int
i
=
0
;
i
<
levelSize
;
++
i
)
{
43
struct
TreeNode
*
node
=
dequeue
(
)
;
44
if
(
node
->
val
>
levelMax
)
{
45
levelMax
=
node
->
val
;
46
}
47
if
(
node
->
left
)
{
48
enqueue
(
node
->
left
)
;
49
}
50
if
(
node
->
right
)
{
51
enqueue
(
node
->
right
)
;
52
}
53
}
54
result
[
(
*
returnSize
)
++
]
=
levelMax
;
55
}
56
57
return
result
;
58
}
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

Explanation

This C++ solution employs a depth-first search (DFS) paradigm using recursion. We track the depth to associate the maximum value found at each level. If a given depth isn’t present in the list of maximum values, it initializes it; otherwise, it updates the level’s maximum with the current node’s value if it exceeds the current maximum.