/
199. Binary Tree Right Side View.cpp
51 lines (47 loc) · 1.4 KB
/
199. Binary Tree Right Side View.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
41
42
43
44
45
46
47
48
49
50
51
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
struct QueueElement {
TreeNode *node;
int depth;
QueueElement(TreeNode *n, int d) :node(n), depth(d) {}
};
public:
vector<int> rightSideView(TreeNode* root) {
// prepare the response
vector<int> res;
if (root == nullptr) return res;
// BFS; adding items into res
queue<QueueElement> q;
q.push(QueueElement(root, 0));
while (!q.empty()) {
// the node
QueueElement qe = q.front();
q.pop();
TreeNode *leftDesc = qe.node->left;
TreeNode *rightDesc = qe.node->right;
int value = qe.node->val;
int d = qe.depth;
// add a new element into res or update an existent element
if (res.size() == d) {
res.push_back(value);
}
else { // update an existent one
res[d] = value;
}
// add descendants into the queue
if (leftDesc) q.push(QueueElement(leftDesc, d+1));
if (rightDesc) q.push(QueueElement(rightDesc, d+1));
}
// return res
return res;
}
};