-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path199.cpp
More file actions
27 lines (26 loc) · 689 Bytes
/
Copy path199.cpp
File metadata and controls
27 lines (26 loc) · 689 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
26
27
class Solution {
public:
vector<int> view;
vector<int> rightSideView(TreeNode *root) {
if (!root)
return {};
view.clear();
queue<TreeNode *> que;
que.push(root);
int size = 1, index = 1;
while (!que.empty()) {
TreeNode *temp = que.front();
if (view.size() < index)
view.push_back(temp->val);
que.pop();
if (temp->right) que.push(temp->right);
if (temp->left) que.push(temp->left);
--size;
if (size == 0) {
size = que.size();
++index;
}
}
return view;
}
};