/**
* 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) {
queue<TreeNode*> q;
vector<vector<int>> res;
if(root!=NULL) q.push(root);
while(!q.empty()){
int len=q.size();
vector<int> vec;
for(int i=0;i<len;i++){
TreeNode* cur=q.front();
q.pop();
int curN=cur->val;
vec.push_back(curN);
if(cur->left!=NULL) q.push(cur->left);
if(cur->right!=NULL) q.push(cur->right);
}
res.push_back(vec);
}
vector<int> result;
for(int i=0;i<res.size();i++){
int max=INT_MIN;
for(int j=0;j<res[i].size();j++){
if(max<res[i][j]){
max=res[i][j];
}
}
result.push_back(max);
}
return result;
}
};
leetcode 515.在每个树行中找最大值
于 2025-03-20 16:10:06 首次发布