199 Binary Tree Right Side View
1 <---
/ \
2 3 <---
\ \
5 4 <---vector<int> rightSideView(TreeNode* root) {
vector<int> right_side;
if (!root) return right_side;
queue<TreeNode*> q;
q.push(root);
q.push(nullptr);
while (!q.empty()) {
TreeNode *front = q.front(); q.pop();
if (front) {
if (front->left) q.push(front->left);
if (front->right) q.push(front->right);
if (!q.front())
right_side.push_back(front->val);
}
else if (!front && !q.empty()) {
q.push(nullptr);
}
}
return right_side;
}Last updated