← Home

515. Find Largest Value in Each Tree Row

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
515

The trick here is to name the state correctly, then let the implementation follow. For 515. Find Largest Value in Each Tree Row, the solution in this repository is mainly a two pointers solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: two pointers, sliding window.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are largestValues.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The queue gives the solution a level-by-level or frontier-style traversal.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 8 ms, faster than 99.28% of C++ online submissions for Find Largest Value in Each Tree Row.
02//Memory Usage: 18.4 MB, less than 100.00% of C++ online submissions for Find Largest Value in Each Tree Row.
03/**
04 * Definition for a binary tree node.
05 * struct TreeNode {
06 *     int val;
07 *     TreeNode *left;
08 *     TreeNode *right;
09 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10 * };
11 */
12class Solution {
13public:
14    vector<int> largestValues(TreeNode* root) {
15        if(!root) return vector<int>();
16        queue<TreeNode*> q;
17        TreeNode* cur;
18        vector<int> ans;
19        int levelCount = 0, nextLevelCount = 0, levelMax = INT_MIN;
20        
21        q.push(root);
22        levelCount = 1;
23        
24        while(!q.empty()){
25            cur = q.front(); q.pop();
26            
27            levelMax = max(levelMax, cur->val);
28            
29            if(cur->left){
30                q.push(cur->left);
31                nextLevelCount++;
32            }
33            if(cur->right){
34                q.push(cur->right);
35                nextLevelCount++;
36            }
37            
38            levelCount--;
39            if(levelCount == 0){
40                ans.push_back(levelMax);
41                levelCount = nextLevelCount;
42                nextLevelCount = 0;
43                levelMax = INT_MIN;
44            }
45        }
46        
47        return ans;
48    }
49};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.