0515.cpp (602B)
1 class Solution { 2 public: 3 vector<int> largestValues(const TreeNode *root) { 4 if (!root) return {}; 5 6 vector<int> res; 7 queue<const TreeNode *> q({root}); 8 while (!q.empty()) { 9 int maxi = INT_MIN; 10 for (int k = q.size(); k > 0; k--) { 11 const TreeNode *root = q.front(); 12 q.pop(); 13 maxi = max(maxi, root->val); 14 if (root->left) q.push(root->left); 15 if (root->right) q.push(root->right); 16 } 17 res.push_back(maxi); 18 } 19 return res; 20 } 21 };