-
Notifications
You must be signed in to change notification settings - Fork 52
/
All Nodes Distance K in Binary Tree.cpp
118 lines (109 loc) · 3.06 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
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
// Solution 1: O(n) Time, O(n) Space
/**
* 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> ans;
unordered_map<TreeNode*, TreeNode*> parent; // son=>parent
unordered_set<TreeNode*> visit; //record visied node
void findParent(TreeNode* node) {
if (node == nullptr) return;
if (node->left != nullptr) {
parent[node->left] = node;
findParent(node->left);
}
if (node->right != nullptr){
parent[node->right] = node;
findParent(node->right);
}
}
void dfs(TreeNode* node, int K){
if (node == nullptr || visit.count(node) > 0) {
return;
}
visit.insert(node);
if (K == 0) {
ans.push_back(node->val);
return;
}
dfs(node->left, K-1);
dfs(node->right, K-1);
dfs(parent[node], K-1);
}
vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
findParent(root);
dfs(target, K);
return ans;
}
};
// Solution 2: O(n) Time, O(lgn) Space
/**
* 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 {
bool FindPath(TreeNode* root, TreeNode* target, vector<bool>& paths) {
if (root == nullptr) {
return false;
}
if (target == root) {
return true;
}
bool l = FindPath(root->left, target, paths);
if (l) {
paths.push_back(false);
return true;
}
bool r = FindPath(root->right, target, paths);
if (r) {
paths.push_back(true);
return true;
}
return false;
}
void FindAnswer(TreeNode* root, vector<int>& ans, int dis, int K) {
if (root == nullptr || dis > K) {
return;
}
if (dis == K) {
ans.push_back(root->val);
return;
}
FindAnswer(root->left, ans, dis + 1, K);
FindAnswer(root->right, ans, dis + 1, K);
}
public:
vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
vector<bool> paths;
FindPath(root, target, paths);
reverse(paths.begin(), paths.end());
vector<int> ans;
for (int i = 0; i < paths.size(); i++) {
if (paths.size() - i == K) {
ans.push_back(root->val);
}
bool path = paths[i];
if (path == false) {
FindAnswer(root->right, ans, paths.size() + 1 - i, K);
root = root->left;
} else {
FindAnswer(root->left, ans, paths.size() + 1 - i, K);
root = root->right;
}
}
FindAnswer(root, ans, 0, K);
return ans;
}
};