-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLC863.cpp
More file actions
183 lines (147 loc) · 4.66 KB
/
LC863.cpp
File metadata and controls
183 lines (147 loc) · 4.66 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
863. All Nodes Distance K in Binary Tree
Given the root of a binary tree, the value of a target node target, and an integer k, return an array of the values of all nodes that have a distance k from the target node.
You can return the answer in any order.
Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
Output: [7,4,1]
Explanation: The nodes that are a distance 2 from the target node (with value 5) have values 7, 4, and 1.
*/
// Program 1, neat code
/**
* 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 {
public:
Solution() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
void markParents(TreeNode* root, unordered_map<TreeNode*, TreeNode*>& parents) {
std::queue<TreeNode*> Q;
Q.push(root);
while(!Q.empty()) {
TreeNode* current = Q.front();
Q.pop();
if (current->left) {
parents[current->left] = current;
Q.push(current->left);
}
if (current->right) {
parents[current->right] = current;
Q.push(current->right);
}
}
}
vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
std::vector<int> result;
std::unordered_map<TreeNode*, TreeNode*> parents;
markParents(root, parents);
std::unordered_set<TreeNode *> visited;
std::queue<std::pair<TreeNode *, int>> Q;
Q.push({target, 0});
visited.insert(target);
while(!Q.empty()) {
TreeNode* node = Q.front().first;
int dist = Q.front().second;
Q.pop();
if (dist == k) {
result.push_back(node->val);
}
if (dist > k)
break;
for (TreeNode* neighbour : {node->left, node->right, parents[node]}) {
if (neighbour && visited.find(neighbour) == visited.end()) {
visited.insert(neighbour);
Q.push({neighbour, dist + 1});
}
}
}
return result;
}
};
// Helper to build sample tree and test
int main() {
TreeNode* root = new TreeNode(3);
root->left = new TreeNode(5);
root->right = new TreeNode(1);
root->left->left = new TreeNode(6);
root->left->right = new TreeNode(2);
root->right->left = new TreeNode(0);
root->right->right = new TreeNode(8);
root->left->right->left = new TreeNode(7);
root->left->right->right = new TreeNode(4);
TreeNode* target = root->left; // Node with value 5
int k = 2;
vector<int> res = distanceK(root, target, k);
for (int val : res) cout << val << " ";
cout << endl;
return 0;
}
// Solution 2: in an another way
class Solution {
public:
Solution() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
std::unordered_map<TreeNode*, TreeNode*> parentsMap;
std::queue<TreeNode*> Q;
Q.push(root);
std::unordered_map<TreeNode*, int> seen;
int distance = 0;
//Step1: Build the parents map using Breadth First Search
while (!Q.empty()) {
int sz = Q.size();
for (int i=0; i < sz; ++i)
{
TreeNode* current = Q.front();
Q.pop();
if (current->left != nullptr) {
Q.push(current->left);
parentsMap[current->left] = current;
}
if (current->right != nullptr) {
Q.push(current->right);
parentsMap[current->right] = current;
}
}
}
Q.push(target);
seen[target] = 1;
// step2: find the distance using above parentsMap
while (!Q.empty() && distance < k) {
int sz = Q.size();
distance++;
for (int i = 0; i < sz; ++i) {
TreeNode* current = Q.front();
Q.pop();
if (current->left != nullptr && seen.find(current->left) == seen.end()) {
Q.push(current->left);
seen[current->left] = 1;
}
if (current->right != nullptr && seen.find(current->right) == seen.end()) {
Q.push(current->right);
seen[current->right] = 1;
}
if (parentsMap.find(current) != parentsMap.end() && seen.find(parentsMap[current]) == seen.end()) {
Q.push(parentsMap[current]);
seen[parentsMap[current]] = 1;
}
}
}
std::vector<int> result;
while(!Q.empty()) {
TreeNode* node = Q.front();
Q.pop();
result.push_back(node->val);
}
return result;
}
}