0199.cpp (549B)
1 class Solution { 2 public: 3 vector<int> rightSideView(TreeNode *root) { 4 if (!root) return {}; 5 6 vector<int> res; 7 queue<TreeNode *> q; 8 q.push(root); 9 for (int lvl = 0; !q.empty(); lvl++) { 10 res.push_back(q.front()->val); 11 for (int k = q.size(); k > 0; k--) { 12 TreeNode *root = q.front(); 13 q.pop(); 14 if (root->right) q.push(root->right); 15 if (root->left) q.push(root->left); 16 } 17 } 18 return res; 19 } 20 };