Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
For example:
Given the following binary tree,
1 <--- / \ 2 3 <--- \ \ 5 4 <---
You should return [1, 3, 4]
.
Nothing Special....
vector<int> rightSideView(TreeNode* root) { if(!root) return {}; vector<TreeNode*> nodes; nodes.push_back(root); vector<int> res; while(!nodes.empty()) { vector<TreeNode*> nextLevel; res.push_back(nodes[0]->val); for(int i = 0; i < nodes.size(); ++i) { if(nodes[i]->right) nextLevel.push_back(nodes[i]->right); if(nodes[i]->left) nextLevel.push_back(nodes[i]->left); } nodes.swap(nextLevel); } return res; }