Skip to content

Commit f172e2a

Browse files
authored
Merge pull request #1237 from 0xff-dev/3319
Add solution and test-cases for problem 3319
2 parents 39fe6fa + d4ad594 commit f172e2a

File tree

6 files changed

+137
-25
lines changed

6 files changed

+137
-25
lines changed
Loading
Loading
Loading

leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/README.md

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
# [3319.K-th Largest Perfect Subtree Size in Binary Tree][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
74

8-
**Example 1:**
5+
You are given the `root` of a **binary tree** and an integer `k`.
6+
7+
Return an integer denoting the size of the `kth` **largest perfect binary subtree**, or `-1` if it doesn't exist.
8+
9+
A **perfect binary tree** is a tree where all leaves are on the same level, and every parent has two children.
10+
11+
**Example 1:**
12+
13+
![1](./1.png)
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: root = [5,3,6,5,2,5,7,1,8,null,null,6,8], k = 2
17+
18+
Output: 3
19+
20+
Explanation:
21+
22+
The roots of the perfect binary subtrees are highlighted in black. Their sizes, in non-increasing order are [3, 3, 1, 1, 1, 1, 1, 1].
23+
The 2nd largest size is 3.
1324
```
1425

15-
## 题意
16-
> ...
26+
**Example 2:**
1727

18-
## 题解
28+
![2](./2.png)
1929

20-
### 思路1
21-
> ...
22-
K-th Largest Perfect Subtree Size in Binary Tree
23-
```go
2430
```
31+
Input: root = [1,2,3,4,5,6,7], k = 1
32+
33+
Output: 7
34+
35+
Explanation:
2536
37+
The sizes of the perfect binary subtrees in non-increasing order are [7, 3, 3, 1, 1, 1, 1]. The size of the largest perfect binary subtree is 7.
38+
```
39+
40+
**Example 3:**
41+
42+
![3](./3.png)
43+
44+
```
45+
Input: root = [1,2,3,null,4], k = 3
46+
47+
Output: -1
48+
49+
Explanation:
50+
51+
The sizes of the perfect binary subtrees in non-increasing order are [1, 1]. There are fewer than 3 perfect binary subtrees.
52+
```
2653

2754
## 结语
2855

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
type TreeNode struct {
6+
Val int
7+
Left *TreeNode
8+
Right *TreeNode
9+
}
10+
11+
func Solution(root *TreeNode, k int) int {
12+
var dfs func(*TreeNode) int
13+
count := make(map[int]int)
14+
dfs = func(cur *TreeNode) int {
15+
if cur == nil {
16+
return -1
17+
}
18+
if cur.Left == nil && cur.Right == nil {
19+
count[1]++
20+
// leaf
21+
return 1
22+
}
23+
left := dfs(cur.Left)
24+
right := dfs(cur.Right)
25+
26+
if left != -1 && left == right {
27+
count[left+right+1]++
28+
return left + right + 1
29+
}
30+
return -1
31+
}
32+
dfs(root)
33+
keys := make([]int, 0)
34+
for i := range count {
35+
keys = append(keys, i)
36+
}
37+
38+
sort.Slice(keys, func(i, j int) bool {
39+
return keys[i] > keys[j]
40+
})
41+
var cnt int
42+
for _, n := range keys {
43+
cnt = count[n]
44+
if cnt >= k {
45+
return n
46+
}
47+
k -= cnt
48+
}
49+
return -1
550
}

leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/Solution_test.go

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,70 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs *TreeNode
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", &TreeNode{
18+
Val: 5,
19+
Left: &TreeNode{
20+
Val: 3,
21+
Left: &TreeNode{
22+
Val: 5,
23+
Left: &TreeNode{Val: 1},
24+
Right: &TreeNode{Val: 8},
25+
},
26+
Right: &TreeNode{Val: 2},
27+
},
28+
Right: &TreeNode{
29+
Val: 6,
30+
Left: &TreeNode{
31+
Val: 5,
32+
Left: &TreeNode{Val: 6},
33+
Right: &TreeNode{Val: 8},
34+
},
35+
Right: &TreeNode{Val: 7},
36+
},
37+
}, 2, 3},
38+
{"TestCase1", &TreeNode{
39+
Val: 1,
40+
Left: &TreeNode{
41+
Val: 2,
42+
Left: &TreeNode{Val: 4},
43+
Right: &TreeNode{Val: 5},
44+
},
45+
Right: &TreeNode{
46+
Val: 3,
47+
Left: &TreeNode{Val: 6},
48+
Right: &TreeNode{Val: 7},
49+
},
50+
}, 1, 7},
51+
{"TestCase3", &TreeNode{
52+
Val: 1,
53+
Left: &TreeNode{
54+
Val: 2,
55+
Right: &TreeNode{Val: 4},
56+
},
57+
Right: &TreeNode{Val: 3},
58+
}, 3, -1},
1959
}
2060

2161
// 开始测试
2262
for i, c := range cases {
2363
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
64+
got := Solution(c.inputs, c.k)
2565
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
66+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
67+
c.expect, got, c.inputs, c.k)
2868
}
2969
})
3070
}
3171
}
3272

33-
// 压力测试
73+
// 压力测试
3474
func BenchmarkSolution(b *testing.B) {
3575
}
3676

37-
// 使用案列
77+
// 使用案列
3878
func ExampleSolution() {
3979
}

0 commit comments

Comments
 (0)