Find the rightmost node value at each level of a binary tree. Serialize each level and output the last node of each level.
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> ans(0);
if (NULL == root)
{
return ans;
}
queue<TreeNode *> qList;
qList.push(root);
while (qList.size() > 0)
{
int size = qList.size();
for (int i = 0;i < size;i++)
{
TreeNode * head = qList.front();
if (NULL != head->left)
{
qList.push(head->left);
}
if (NULL != head->right)
{
qList.push(head->right);
}
qList.pop();
if (i == size-1)
{
ans.push_back(head->val);
}
}
}
return ans;
}
};