Skip to content

Commit f8d6749

Browse files
authored
Create number-of-good-leaf-nodes-pairs.cpp
1 parent 382868e commit f8d6749

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Time: O(n)
2+
// Space: O(h)
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* struct TreeNode {
7+
* int val;
8+
* TreeNode *left;
9+
* TreeNode *right;
10+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
11+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
12+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
13+
* };
14+
*/
15+
16+
// Time: O(n)
17+
// Space: O(h)
18+
class Solution {
19+
public:
20+
int countPairs(TreeNode* root, int distance) {
21+
return iter_dfs(distance, root);
22+
}
23+
24+
private:
25+
int iter_dfs(int distance, TreeNode *root) {
26+
using RET = unordered_map<int, int>;
27+
int result = 0;
28+
RET ret;
29+
vector<tuple<int, TreeNode *, shared_ptr<RET>, shared_ptr<RET>, RET *>> stk = {{1, root, nullptr, nullptr, &ret}};
30+
while (!stk.empty()) {
31+
const auto [step, node, left, right, ret] = stk.back(); stk.pop_back();
32+
if (step == 1) {
33+
if (!node) {
34+
continue;
35+
}
36+
if (!node->left && !node->right) {
37+
(*ret)[0] = 1;
38+
continue;
39+
}
40+
const auto& left = make_shared<RET>(), &right = make_shared<RET>();
41+
stk.emplace_back(2, nullptr, left, right, ret);
42+
stk.emplace_back(1, node->right, nullptr, nullptr, right.get());
43+
stk.emplace_back(1, node->left, nullptr, nullptr, left.get());
44+
} else {
45+
for (const auto& [left_d, left_c] : *left) {
46+
for (const auto& [right_d, right_c] : *right) {
47+
if (left_d + right_d + 2 <= distance) {
48+
result += left_c * right_c;
49+
}
50+
}
51+
}
52+
for (const auto& [left_d, left_c] : *left) {
53+
(*ret)[left_d + 1] += left_c;
54+
}
55+
for (const auto& [right_d, right_c] : *right) {
56+
(*ret)[right_d + 1] += right_c;
57+
}
58+
}
59+
}
60+
return result;
61+
}
62+
};
63+
64+
65+
// Time: O(n)
66+
// Space: O(h)
67+
class Solution2 {
68+
public:
69+
int countPairs(TreeNode* root, int distance) {
70+
return dfs(distance, root).first;
71+
}
72+
73+
private:
74+
pair<int, unordered_map<int, int>> dfs(int distance, TreeNode *node) {
75+
if (!node) {
76+
return {0, {}};
77+
}
78+
if (!node->left && !node->right) {
79+
return {0, {{0, 1}}};
80+
}
81+
const auto& left = dfs(distance, node->left);
82+
const auto& right = dfs(distance, node->right);
83+
int result = left.first + right.first;
84+
for (const auto& [left_d, left_c] : left.second) {
85+
for (const auto& [right_d, right_c] : right.second) {
86+
if (left_d + right_d + 2 <= distance) {
87+
result += left_c * right_c;
88+
}
89+
}
90+
}
91+
unordered_map<int, int> count;
92+
for (const auto& [left_d, left_c] : left.second) {
93+
count[left_d + 1] += left_c;
94+
}
95+
for (const auto& [right_d, right_c] : right.second) {
96+
count[right_d + 1] += right_c;
97+
}
98+
return {result, count};
99+
}
100+
};

0 commit comments

Comments
 (0)