Skip to content

Instantly share code, notes, and snippets.

@shitu13
Created May 21, 2024 05:50
Show Gist options
  • Save shitu13/e5c76a040745fdab5de25630ec19e56a to your computer and use it in GitHub Desktop.
Save shitu13/e5c76a040745fdab5de25630ec19e56a to your computer and use it in GitHub Desktop.
Binary Tree Right Side View
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> res;
if (root == NULL)
return {};
queue<TreeNode*> que;
que.push(root);
while (!que.empty()) {
int n = que.size();
TreeNode* node = NULL;
while (n--) {
node = que.front();
que.pop();
if (node->left)
que.push(node->left);
if (node->right)
que.push(node->right);
}
res.push_back(node->val);
}
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment