-
Notifications
You must be signed in to change notification settings - Fork 0
/
515-Find-Largest-Value-in-Each-Tree-Row.cpp
70 lines (62 loc) · 1.89 KB
/
515-Find-Largest-Value-in-Each-Tree-Row.cpp
1
2
3
4
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Author : Vicen-te
// Date : 10/25/2023 (MM-DD-YYYY)
/**
*
* Description:
* Given the root of a binary tree, return an array of the largest value in each row of the
* tree (0-indexed).
*
* Ex1.
* Input: root = [1,3,2,5,3,null,9]
* Output: [1,3,9]
*
* Ex2.
* Input: root = [1,2,3]
* Output: [1,3]
*
* Algorithm:
* 1. Use BFS (Breadth-First Search) with a queue to traverse the binary tree.
* 2. Progress to the next level when all nodes at the current level are explored.
* 3. Track the maximum value within the current level.
* 4. Add the maximum value to the result vector at the end of each level.
* 5. Return the vector containing the maximum values after completing the tree traversal.
*
* Time: O(N)
* Space: O(N)
*
*/
/**
* Definition for a binary tree node.
* 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) {}
* };
*/
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
if(!root) return vector<int>();
std::vector<int> res;
std::queue<TreeNode*> queue;
queue.push(root);
while(queue.size())
{
int size = queue.size();
int max_value = INT_MIN;
while(size--)
{
TreeNode*& node = queue.front();
max_value = max(max_value, node->val);
if(node->left != nullptr) queue.push(node->left);
if(node->right != nullptr) queue.push(node->right);
queue.pop();
}
res.push_back(max_value);
}
return res;
}
};