/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> v;
if (!root) {
return v;
}
queue<TreeNode*> q;
q.push(root);
v.push_back(root->val);
while (!q.empty()) {
int size = q.size();
int tmp = 0;
int flag = 0;
for(int i = 0; i < size; i++) {
TreeNode* node = q.front();
q.pop();
if (node->left) {
q.push(node->left);
tmp = node->left->val;
flag = 1;
}
if (node->right) {
q.push(node->right);
tmp = node->right->val;
flag = 1;
}
}
if (flag){
v.push_back(tmp);
}
}
return v;
}
};