Skip to content

Latest commit

 

History

History
21 lines (21 loc) · 563 Bytes

515. Find Largest Value in Each Tree Row.md

File metadata and controls

21 lines (21 loc) · 563 Bytes
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;
    }
};