-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1530-NumberOfGoodLeafNodesPairs.go
More file actions
193 lines (180 loc) · 5.88 KB
/
1530-NumberOfGoodLeafNodesPairs.go
File metadata and controls
193 lines (180 loc) · 5.88 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
// 1530. Number of Good Leaf Nodes Pairs
// You are given the root of a binary tree and an integer distance.
// A pair of two different leaf nodes of a binary tree is said to be good if the length of the shortest path between them is less than or equal to distance.
// Return the number of good leaf node pairs in the tree.
// Example 1:
// 1
// / \
// 2 3
// \
// 4
// <img src="https://assets.leetcode.com/uploads/2020/07/09/e1.jpg" />
// Input: root = [1,2,3,null,4], distance = 3
// Output: 1
// Explanation: The leaf nodes of the tree are 3 and 4 and the length of the shortest path between them is 3. This is the only good pair.
// Example 2:
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
// <img src="https://assets.leetcode.com/uploads/2020/07/09/e2.jpg" />
// Input: root = [1,2,3,4,5,6,7], distance = 3
// Output: 2
// Explanation: The good pairs are [4,5] and [6,7] with shortest path = 2. The pair [4,6] is not good because the length of ther shortest path between them is 4.
// Example 3:
// Input: root = [7,1,4,6,null,5,3,null,null,null,null,null,2], distance = 3
// Output: 1
// Explanation: The only good pair is [2,5].
// Constraints:
// The number of nodes in the tree is in the range [1, 2^10].
// 1 <= Node.val <= 100
// 1 <= distance <= 10
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func countPairs(root *TreeNode, distance int) int {
res := 0
var dfs func(*TreeNode) []int
dfs = func(node *TreeNode) []int {
if node == nil { return []int{} }
if node.Left == nil && node.Right == nil { return []int{0} }
arr := make([]int, 0)
left := dfs(node.Left)
for _, l := range left {
if l++; l <= distance {
arr = append(arr, l)
}
}
right := dfs(node.Right)
for _, r := range right {
if r++; r <= distance {
arr = append(arr, r)
}
}
for _, l := range left {
for _, r := range right {
if l+r+2 <= distance { // a与left b与right+2
res++
}
}
}
return arr
}
dfs(root)
return res
}
func countPairs1(root *TreeNode, distance int) int {
var dfs func(*TreeNode) ([]int, int)
dfs = func (node *TreeNode) ([]int, int) {
if node == nil {
return []int{}, 0
}
if node.Left == nil && node.Right == nil {
return []int{1}, 0
}
lLeaves, lCount := dfs(node.Left)
rLeaves, rCount := dfs(node.Right)
count := lCount + rCount
for _, left := range lLeaves {
for _, right := range rLeaves {
if left + right <= distance {
count++
}
}
}
leaves := make([]int, 0, len(lLeaves) + len(rLeaves))
for _, leaf := range lLeaves {
if leaf < 9 {
leaves = append(leaves, leaf + 1)
}
}
for _, leaf := range rLeaves {
if leaf < 9 {
leaves = append(leaves, leaf + 1)
}
}
return leaves, count
}
_, res := dfs(root)
return res
}
func main() {
// Example 1:
// 1
// / \
// 2 3
// \
// 4
// <img src="https://assets.leetcode.com/uploads/2020/07/09/e1.jpg" />
// Input: root = [1,2,3,null,4], distance = 3
// Output: 1
// Explanation: The leaf nodes of the tree are 3 and 4 and the length of the shortest path between them is 3. This is the only good pair.
tree1 := &TreeNode {
1,
&TreeNode { 2, nil, &TreeNode { 4, nil, nil }, },
&TreeNode { 3, nil, nil },
}
fmt.Println(countPairs(tree1, 3)) // 1
// Example 2:
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
// <img src="https://assets.leetcode.com/uploads/2020/07/09/e2.jpg" />
// Input: root = [1,2,3,4,5,6,7], distance = 3
// Output: 2
// Explanation: The good pairs are [4,5] and [6,7] with shortest path = 2. The pair [4,6] is not good because the length of ther shortest path between them is 4.
tree2 := &TreeNode{
1,
&TreeNode{ 2, &TreeNode{ 4, nil, nil }, &TreeNode{ 5, nil, nil }, },
&TreeNode{ 3, &TreeNode{ 6, nil, nil }, &TreeNode{ 7, nil, nil }, },
}
fmt.Println(countPairs(tree2, 3)) // 2
// Example 3:
// Input: root = [7,1,4,6,null,5,3,null,null,null,null,null,2], distance = 3
// Output: 1
// Explanation: The only good pair is [2,5].
tree3 := &TreeNode{
7,
&TreeNode{ 1, &TreeNode{ 6, nil, nil }, nil, },
&TreeNode{ 4, &TreeNode{ 5, nil, nil }, &TreeNode{ 3, nil, &TreeNode{ 2, nil, nil }, }, },
}
fmt.Println(countPairs(tree3, 3)) // 1
tree11 := &TreeNode {
1,
&TreeNode { 2, nil, &TreeNode { 4, nil, nil }, },
&TreeNode { 3, nil, nil },
}
fmt.Println(countPairs1(tree11, 3)) // 1
tree12 := &TreeNode{
1,
&TreeNode{ 2, &TreeNode{ 4, nil, nil }, &TreeNode{ 5, nil, nil }, },
&TreeNode{ 3, &TreeNode{ 6, nil, nil }, &TreeNode{ 7, nil, nil }, },
}
fmt.Println(countPairs1(tree12, 3)) // 2
// Example 3:
// Input: root = [7,1,4,6,null,5,3,null,null,null,null,null,2], distance = 3
// Output: 1
// Explanation: The only good pair is [2,5].
tree13 := &TreeNode{
7,
&TreeNode{ 1, &TreeNode{ 6, nil, nil }, nil, },
&TreeNode{ 4, &TreeNode{ 5, nil, nil }, &TreeNode{ 3, nil, &TreeNode{ 2, nil, nil }, }, },
}
fmt.Println(countPairs1(tree13, 3)) // 1
}