-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy path0199.cpp
More file actions
25 lines (24 loc) · 682 Bytes
/
0199.cpp
File metadata and controls
25 lines (24 loc) · 682 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
vector<int> rightSideView(TreeNode* root)
{
if (root == nullptr) return {};
vector<int> res;
deque<TreeNode*> s;
s.push_back(root);
while (!s.empty())
{
int n = s.size();
res.push_back(s.back()->val);
for (int i = 0; i < n; ++i)
{
TreeNode* node = s.front(); s.pop_front();
if (node->left) s.push_back(node->left);
if (node->right) s.push_back(node->right);
}
}
return res;
}
};