-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily31.cpp
More file actions
146 lines (119 loc) · 3.74 KB
/
Copy pathdaily31.cpp
File metadata and controls
146 lines (119 loc) · 3.74 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
// Solution 1
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void dfs(TreeNode* curr, std::unordered_map<std::string, TreeNode*>& leafs, string& path) {
if (curr == nullptr)
return;
if (curr->left == nullptr and curr->right == nullptr) {
leafs[path] = curr;
return;
}
path += "L";
dfs(curr->left, leafs, path);
path.pop_back();
path += "R";
dfs(curr->right, leafs, path);
path.pop_back();
return;
}
int countPairs(TreeNode* root, int distance) {
// find all leaf nodes
// find distance to other leaf nodes given they are within distance
// compare paths of one leaf node to another
// = amount of differing nodes in the paths
// + remaining path lengths for both nodes after difference in path is detected
int pairs = 0;
auto leafs = std::unordered_map<std::string, TreeNode*>{};
auto visited = unordered_set<TreeNode*>{};
std::string path{""};
dfs(root, leafs, path);
for (auto [p, n] : leafs) {
for (auto [o_p, o_n] : leafs) {
if (visited.contains(o_n))
continue;
if (o_n == n)
continue;
auto difference = 0;
auto index = 0;
while (index < p.size() and index < o_p.size()) {
if (p[index] != o_p[index]) {
difference += 2;
difference += std::max(p.size(), o_p.size()) - (index + 1);
difference += std::min(p.size(), o_p.size()) - (index + 1);
break;
}
index++;
}
if (difference <= distance) {
pairs++;
}
}
visited.insert(n);
}
return pairs;
}
};
// Solution 2
struct NodePos
{
int64_t depth;
int64_t pos;
};
void dfs(TreeNode *node, std::vector<NodePos> &leaves, int64_t depth, int64_t pos)
{
if(node->left != nullptr)
{
dfs(node->left, leaves, depth + 1, pos);
}
if(node->right != nullptr)
{
dfs(node->right, leaves, depth + 1, pos | (int64_t(1) << depth));
}
if(node->left == nullptr && node->right == nullptr)
{
NodePos p;
p.depth = depth;
p.pos = pos;
leaves.emplace_back(p);
}
}
class Solution {
public:
int countPairs(TreeNode* root, int distance)
{
std::vector<NodePos> leaves;
dfs(root, leaves, 0, 0);
int result = 0;
std::cout << "=========" << std::endl;
for(size_t i = 0; i < leaves.size() - 1; i++)
{
for(size_t j = i + 1; j < leaves.size(); j++)
{
auto fd = leaves[i].depth;
auto sd = leaves[j].depth;
auto fp = leaves[i].pos;
auto sp = leaves[j].pos;
auto min = std::min(fd, sd);
auto depthDiff = __builtin_ctzl(fp ^ sp);
auto diff = (fd - depthDiff) + sd - depthDiff;
if(diff == 0 && fd != sd)
{
diff = std::abs(fd - sd);
}
result += diff <= distance;
}
}
return result;
}
};