-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathsolution.cpp
40 lines (40 loc) · 912 Bytes
/
solution.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
vector<int> rightSideView(TreeNode *root)
{
vector<int> ans;
if (root == NULL)
return ans;
queue<TreeNode*> que;
que.push(root);
TreeNode* curr;
while(!que.empty())
{
int cnt = que.size();
for (int i = 0; i < cnt; i++)
{
curr = que.front(); que.pop();
if (curr->left)
{
que.push(curr->left);
}
if (curr->right)
{
que.push(curr->right);
}
}
ans.push_back(curr->val);
}
return ans;
}
};