-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAll Nodes Distance K in Binary Tree.cpp
59 lines (50 loc) · 1.49 KB
/
All Nodes Distance K in Binary Tree.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
52
53
54
55
56
57
58
59
/**
* 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:
vector <int> graph[1005];
set <int> vis;
void build(TreeNode *root) {
if(!root) return;
if(root->left) {
graph[root->val].push_back(root->left->val);
graph[root->left->val].push_back(root->val);
}
if(root->right) {
graph[root->val].push_back(root->right->val);
graph[root->right->val].push_back(root->val);
}
build(root->left);
build(root->right);
}
void bfs(vector <int> &ans, int x, int K) {
queue < pair<int, int> > q;
q.push({ x, 0 });
while(!q.empty()) {
pair <int, int> top = q.front();
q.pop();
int node = top.first, level = top.second;
vis.insert(node);
if(level == K) {
ans.push_back(node);
}
for(int i=0;i<graph[node].size();i++) {
if(vis.find(graph[node][i]) == vis.end())
q.push({ graph[node][i], level + 1 });
}
}
}
vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
build(root);
vector <int> ans;
bfs(ans, target->val, K);
return ans;
}
};