Use a "last" and "tail" to track the rightmost val in each level of the binary tree.

```
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
int last=0,tail=0;
vector<int> solution;
queue<TreeNode* > bfs;
if(root==NULL)
return solution;
bfs.push(root);
last=root->val;
tail=root->val;
while(bfs.size()!=0){
TreeNode* v=bfs.front();
bfs.pop();
if(v->left!=NULL){
bfs.push(v->left);
tail=v->left->val;
}
if(v->right!=NULL){
bfs.push(v->right);
tail=v->right->val;
}
if(v->val==last){
solution.push_back(v->val);
last=tail;
}
}
return solution;
}
```

};