Skip to content

Commit 84a2672

Browse files
committed
feat: add solutions to lc problem: No.1530
No.1530.Number of Good Leaf Nodes Pairs
1 parent 9c282cc commit 84a2672

File tree

6 files changed

+494
-2
lines changed

6 files changed

+494
-2
lines changed

solution/1500-1599/1530.Number of Good Leaf Nodes Pairs/README.md

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,194 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
**方法一:递归**
71+
72+
题目求一个二叉树好叶子节点的对数,答案可以拆分为三部分之和:左子树好叶子节点的对数、右子树好叶子节点的对数,以及左子树叶子节点与右子树叶子节点组成的好叶子节点的对数。
73+
74+
递归求解即可。
75+
76+
时间复杂度 $O(n \times distance^2 \times h)$,其中 $n$ 是二叉树的节点数,而 $h$ 是二叉树的高度。
77+
7078
<!-- tabs:start -->
7179

7280
### **Python3**
7381

7482
<!-- 这里可写当前语言的特殊实现逻辑 -->
7583

7684
```python
77-
85+
# Definition for a binary tree node.
86+
# class TreeNode:
87+
# def __init__(self, val=0, left=None, right=None):
88+
# self.val = val
89+
# self.left = left
90+
# self.right = right
91+
class Solution:
92+
def countPairs(self, root: TreeNode, distance: int) -> int:
93+
def dfs(root, cnt, i):
94+
if root is None or i >= distance:
95+
return
96+
if root.left is None and root.right is None:
97+
cnt[i] += 1
98+
return
99+
dfs(root.left, cnt, i + 1)
100+
dfs(root.right, cnt, i + 1)
101+
102+
if root is None:
103+
return 0
104+
ans = self.countPairs(root.left, distance) + \
105+
self.countPairs(root.right, distance)
106+
cnt1 = Counter()
107+
cnt2 = Counter()
108+
dfs(root.left, cnt1, 1)
109+
dfs(root.right, cnt2, 1)
110+
111+
for k1, v1 in cnt1.items():
112+
for k2, v2 in cnt2.items():
113+
if k1 + k2 <= distance:
114+
ans += v1 * v2
115+
return ans
78116
```
79117

80118
### **Java**
81119

82120
<!-- 这里可写当前语言的特殊实现逻辑 -->
83121

84122
```java
123+
/**
124+
* Definition for a binary tree node.
125+
* public class TreeNode {
126+
* int val;
127+
* TreeNode left;
128+
* TreeNode right;
129+
* TreeNode() {}
130+
* TreeNode(int val) { this.val = val; }
131+
* TreeNode(int val, TreeNode left, TreeNode right) {
132+
* this.val = val;
133+
* this.left = left;
134+
* this.right = right;
135+
* }
136+
* }
137+
*/
138+
class Solution {
139+
public int countPairs(TreeNode root, int distance) {
140+
if (root == null) {
141+
return 0;
142+
}
143+
int ans = countPairs(root.left, distance) + countPairs(root.right, distance);
144+
int[] cnt1 = new int[distance];
145+
int[] cnt2 = new int[distance];
146+
dfs(root.left, cnt1, 1);
147+
dfs(root.right, cnt2, 1);
148+
for (int i = 0; i < distance; ++i) {
149+
for (int j = 0; j < distance; ++j) {
150+
if (i + j <= distance) {
151+
ans += cnt1[i] * cnt2[j];
152+
}
153+
}
154+
}
155+
return ans;
156+
}
157+
158+
void dfs(TreeNode root, int[] cnt, int i) {
159+
if (root == null || i >= cnt.length) {
160+
return;
161+
}
162+
if (root.left == null && root.right == null) {
163+
++cnt[i];
164+
return;
165+
}
166+
dfs(root.left, cnt, i + 1);
167+
dfs(root.right, cnt, i + 1);
168+
}
169+
}
170+
```
171+
172+
### **C++**
173+
174+
```cpp
175+
/**
176+
* Definition for a binary tree node.
177+
* struct TreeNode {
178+
* int val;
179+
* TreeNode *left;
180+
* TreeNode *right;
181+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
182+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
183+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
184+
* };
185+
*/
186+
class Solution {
187+
public:
188+
int countPairs(TreeNode* root, int distance) {
189+
if (!root) return 0;
190+
int ans = countPairs(root->left, distance) + countPairs(root->right, distance);
191+
vector<int> cnt1(distance);
192+
vector<int> cnt2(distance);
193+
dfs(root->left, cnt1, 1);
194+
dfs(root->right, cnt2, 1);
195+
for (int i = 0; i < distance; ++i) {
196+
for (int j = 0; j < distance; ++j) {
197+
if (i + j <= distance) {
198+
ans += cnt1[i] * cnt2[j];
199+
}
200+
}
201+
}
202+
return ans;
203+
}
204+
205+
void dfs(TreeNode* root, vector<int>& cnt, int i) {
206+
if (!root || i >= cnt.size()) return;
207+
if (!root->left && !root->right) {
208+
++cnt[i];
209+
return;
210+
}
211+
dfs(root->left, cnt, i + 1);
212+
dfs(root->right, cnt, i + 1);
213+
}
214+
};
215+
```
85216
217+
### **Go**
218+
219+
```go
220+
/**
221+
* Definition for a binary tree node.
222+
* type TreeNode struct {
223+
* Val int
224+
* Left *TreeNode
225+
* Right *TreeNode
226+
* }
227+
*/
228+
func countPairs(root *TreeNode, distance int) int {
229+
if root == nil {
230+
return 0
231+
}
232+
ans := countPairs(root.Left, distance) + countPairs(root.Right, distance)
233+
cnt1 := make([]int, distance)
234+
cnt2 := make([]int, distance)
235+
dfs(root.Left, cnt1, 1)
236+
dfs(root.Right, cnt2, 1)
237+
for i, v1 := range cnt1 {
238+
for j, v2 := range cnt2 {
239+
if i+j <= distance {
240+
ans += v1 * v2
241+
}
242+
}
243+
}
244+
return ans
245+
}
246+
247+
func dfs(root *TreeNode, cnt []int, i int) {
248+
if root == nil || i >= len(cnt) {
249+
return
250+
}
251+
if root.Left == nil && root.Right == nil {
252+
cnt[i]++
253+
return
254+
}
255+
dfs(root.Left, cnt, i+1)
256+
dfs(root.Right, cnt, i+1)
257+
}
86258
```
87259

88260
### **...**

solution/1500-1599/1530.Number of Good Leaf Nodes Pairs/README_EN.md

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,177 @@
4949
### **Python3**
5050

5151
```python
52-
52+
# Definition for a binary tree node.
53+
# class TreeNode:
54+
# def __init__(self, val=0, left=None, right=None):
55+
# self.val = val
56+
# self.left = left
57+
# self.right = right
58+
class Solution:
59+
def countPairs(self, root: TreeNode, distance: int) -> int:
60+
def dfs(root, cnt, i):
61+
if root is None or i >= distance:
62+
return
63+
if root.left is None and root.right is None:
64+
cnt[i] += 1
65+
return
66+
dfs(root.left, cnt, i + 1)
67+
dfs(root.right, cnt, i + 1)
68+
69+
if root is None:
70+
return 0
71+
ans = self.countPairs(root.left, distance) + \
72+
self.countPairs(root.right, distance)
73+
cnt1 = Counter()
74+
cnt2 = Counter()
75+
dfs(root.left, cnt1, 1)
76+
dfs(root.right, cnt2, 1)
77+
78+
for k1, v1 in cnt1.items():
79+
for k2, v2 in cnt2.items():
80+
if k1 + k2 <= distance:
81+
ans += v1 * v2
82+
return ans
5383
```
5484

5585
### **Java**
5686

5787
```java
88+
/**
89+
* Definition for a binary tree node.
90+
* public class TreeNode {
91+
* int val;
92+
* TreeNode left;
93+
* TreeNode right;
94+
* TreeNode() {}
95+
* TreeNode(int val) { this.val = val; }
96+
* TreeNode(int val, TreeNode left, TreeNode right) {
97+
* this.val = val;
98+
* this.left = left;
99+
* this.right = right;
100+
* }
101+
* }
102+
*/
103+
class Solution {
104+
public int countPairs(TreeNode root, int distance) {
105+
if (root == null) {
106+
return 0;
107+
}
108+
int ans = countPairs(root.left, distance) + countPairs(root.right, distance);
109+
int[] cnt1 = new int[distance];
110+
int[] cnt2 = new int[distance];
111+
dfs(root.left, cnt1, 1);
112+
dfs(root.right, cnt2, 1);
113+
for (int i = 0; i < distance; ++i) {
114+
for (int j = 0; j < distance; ++j) {
115+
if (i + j <= distance) {
116+
ans += cnt1[i] * cnt2[j];
117+
}
118+
}
119+
}
120+
return ans;
121+
}
122+
123+
void dfs(TreeNode root, int[] cnt, int i) {
124+
if (root == null || i >= cnt.length) {
125+
return;
126+
}
127+
if (root.left == null && root.right == null) {
128+
++cnt[i];
129+
return;
130+
}
131+
dfs(root.left, cnt, i + 1);
132+
dfs(root.right, cnt, i + 1);
133+
}
134+
}
135+
```
136+
137+
### **C++**
138+
139+
```cpp
140+
/**
141+
* Definition for a binary tree node.
142+
* struct TreeNode {
143+
* int val;
144+
* TreeNode *left;
145+
* TreeNode *right;
146+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
147+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
148+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
149+
* };
150+
*/
151+
class Solution {
152+
public:
153+
int countPairs(TreeNode* root, int distance) {
154+
if (!root) return 0;
155+
int ans = countPairs(root->left, distance) + countPairs(root->right, distance);
156+
vector<int> cnt1(distance);
157+
vector<int> cnt2(distance);
158+
dfs(root->left, cnt1, 1);
159+
dfs(root->right, cnt2, 1);
160+
for (int i = 0; i < distance; ++i) {
161+
for (int j = 0; j < distance; ++j) {
162+
if (i + j <= distance) {
163+
ans += cnt1[i] * cnt2[j];
164+
}
165+
}
166+
}
167+
return ans;
168+
}
169+
170+
void dfs(TreeNode* root, vector<int>& cnt, int i) {
171+
if (!root || i >= cnt.size()) return;
172+
if (!root->left && !root->right) {
173+
++cnt[i];
174+
return;
175+
}
176+
dfs(root->left, cnt, i + 1);
177+
dfs(root->right, cnt, i + 1);
178+
}
179+
};
180+
```
58181
182+
### **Go**
183+
184+
```go
185+
/**
186+
* Definition for a binary tree node.
187+
* type TreeNode struct {
188+
* Val int
189+
* Left *TreeNode
190+
* Right *TreeNode
191+
* }
192+
*/
193+
func countPairs(root *TreeNode, distance int) int {
194+
if root == nil {
195+
return 0
196+
}
197+
ans := countPairs(root.Left, distance) + countPairs(root.Right, distance)
198+
cnt1 := make([]int, distance)
199+
cnt2 := make([]int, distance)
200+
dfs(root.Left, cnt1, 1)
201+
dfs(root.Right, cnt2, 1)
202+
for i, v1 := range cnt1 {
203+
for j, v2 := range cnt2 {
204+
if i+j <= distance {
205+
ans += v1 * v2
206+
}
207+
}
208+
}
209+
return ans
210+
}
211+
212+
func dfs(root *TreeNode, cnt []int, i int) {
213+
if root == nil || i >= len(cnt) {
214+
return
215+
}
216+
if root.Left == nil && root.Right == nil {
217+
cnt[i]++
218+
return
219+
}
220+
dfs(root.Left, cnt, i+1)
221+
dfs(root.Right, cnt, i+1)
222+
}
59223
```
60224

61225
### **...**

0 commit comments

Comments
 (0)