```
class Solution {
vector<int> res;
void help(TreeNode* t, int depth){
if (!t) return;
if (res.size()<=depth) res.push_back(t->val);
else res[depth] = max(res[depth],t->val);
help(t->left, depth+1);
help(t->right, depth+1);
}
public:
vector<int> largestValues(TreeNode* root) {
help(root, 0);
return res;
}
};
```