-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallNodesDistanceKinBinaryTree.cpp
More file actions
52 lines (49 loc) · 1.44 KB
/
allNodesDistanceKinBinaryTree.cpp
File metadata and controls
52 lines (49 loc) · 1.44 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
// Source: https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/
// Author: Miao Zhang
// Date: 2021-03-19
/**
* 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> distanceK(TreeNode* root, TreeNode* target, int K) {
buildGraph(nullptr, root);
vector<int> res;
unordered_set<TreeNode*> seen;
queue<TreeNode*> q;
q.push(target);
seen.insert(target);
int k = 0;
while (!q.empty() && k <= K) {
int len = q.size();
while (len--) {
TreeNode* node = q.front();
q.pop();
if (k == K) res.push_back(node->val);
for (TreeNode* child: graph[node]) {
if (seen.count(child)) continue;
seen.insert(child);
q.push(child);
}
}
k++;
}
return res;
}
private:
unordered_map<TreeNode*, vector<TreeNode*>> graph;
void buildGraph(TreeNode* parent, TreeNode* child) {
if (parent) {
graph[parent].push_back(child);
graph[child].push_back(parent);
}
if (child->left) buildGraph(child, child->left);
if (child->right) buildGraph(child, child->right);
}
};