class Solution {
public:
vector<int> largestValues(TreeNode* root) {
if (!root) return {};
vector<int> res;
queue<TreeNode*> q({root});
while (!q.empty()) {
int mx = INT_MIN;
for (int i = q.size(); i > 0; --i) {
TreeNode* node = q.front(); q.pop();
mx = max(mx, node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
res.push_back(mx);
}
return res;
}
};