leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0199.cpp (549B)


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