/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int>ans;
queue<TreeNode*>q;
if(root!=NULL){
q.push(root);
//ans.push_back(root->val);
}
while(!q.empty()){
int k=q.size();
while(k--){
TreeNode*p=q.front();
if(p->left!=NULL){
q.push(p->left);
}
if(p->right!=NULL)q.push(p->right);
if(k==0){
ans.push_back(p->val);
}
q.pop();
}
}
return ans;
}
};
11-17