-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathAll Nodes Distance K in Binary Tree.cpp
61 lines (47 loc) · 1.34 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
60
61
/*
Solution by Rahul Surana
***********************************************************
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.
***********************************************************
*/
/**
* 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:
unordered_map<TreeNode*, TreeNode*> m;
unordered_map<TreeNode*, bool> v;
vector<int> ans;
void df(TreeNode* &r, int k){
if(r == NULL) return;
if(v[r]) return;
v[r] = true;
// cout << r->val <<"\n";
if(k == 0) {
ans.push_back(r->val);
return;
}
df(r->left,k-1);
df(r->right,k-1);
df(m[r],k-1);
}
void dfr(TreeNode* &r, TreeNode* p){
if(!r) return;
m[r] = p;
dfr(r->left,r);
dfr(r->right,r);
}
vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
dfr(root,nullptr);
df(target, k);
return ans;
}
};