-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
61 lines (51 loc) · 1.6 KB
/
Copy pathsolution.cpp
File metadata and controls
61 lines (51 loc) · 1.6 KB
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
52
53
54
55
56
57
58
59
60
61
/*
class Node {
public:
int data;
Node* left;
Node* right;
Node(int x) {
data = x;
left = right = NULL;
}
};
*/
#include <vector>
#include <queue>
#include <unordered_map>
#include <limits>
using namespace std;
class Solution {
public:
vector<int> bottomView(Node *root) {
vector<int> result;
if (!root) return result;
unordered_map<int,int> hdValue; // hd -> last seen node data
queue<pair<Node*, int>> q; // pair of node and its horizontal distance
int minHd = 0, maxHd = 0;
q.push({root, 0});
while (!q.empty()) {
auto p = q.front(); q.pop();
Node* node = p.first;
int hd = p.second;
// Overwrite the value at this horizontal distance.
// Because BFS is level-order, the later visit (deeper / later in same level)
// will replace earlier ones — exactly what bottom view requires.
hdValue[hd] = node->data;
if (node->left) {
q.push({node->left, hd - 1});
if (hd - 1 < minHd) minHd = hd - 1;
}
if (node->right) {
q.push({node->right, hd + 1});
if (hd + 1 > maxHd) maxHd = hd + 1;
}
}
// Collect results from leftmost hd to rightmost hd.
for (int hd = minHd; hd <= maxHd; ++hd) {
// hdValue must contain hd (because visited), so safe to access.
result.push_back(hdValue[hd]);
}
return result;
}
};