Skip to content

Add solution and test-cases for problem 3319 #1237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
# [3319.K-th Largest Perfect Subtree Size in Binary Tree][title]

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

## Description

**Example 1:**
You are given the `root` of a **binary tree** and an integer `k`.

Return an integer denoting the size of the `kth` **largest perfect binary subtree**, or `-1` if it doesn't exist.

A **perfect binary tree** is a tree where all leaves are on the same level, and every parent has two children.

**Example 1:**

![1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: root = [5,3,6,5,2,5,7,1,8,null,null,6,8], k = 2

Output: 3

Explanation:

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].
The 2nd largest size is 3.
```

## 题意
> ...
**Example 2:**

## 题解
![2](./2.png)

### 思路1
> ...
K-th Largest Perfect Subtree Size in Binary Tree
```go
```
Input: root = [1,2,3,4,5,6,7], k = 1

Output: 7

Explanation:

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.
```

**Example 3:**

![3](./3.png)

```
Input: root = [1,2,3,null,4], k = 3

Output: -1

Explanation:

The sizes of the perfect binary subtrees in non-increasing order are [1, 1]. There are fewer than 3 perfect binary subtrees.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func Solution(root *TreeNode, k int) int {
var dfs func(*TreeNode) int
count := make(map[int]int)
dfs = func(cur *TreeNode) int {
if cur == nil {
return -1
}
if cur.Left == nil && cur.Right == nil {
count[1]++
// leaf
return 1
}
left := dfs(cur.Left)
right := dfs(cur.Right)

if left != -1 && left == right {
count[left+right+1]++
return left + right + 1
}
return -1
}
dfs(root)
keys := make([]int, 0)
for i := range count {
keys = append(keys, i)
}

sort.Slice(keys, func(i, j int) bool {
return keys[i] > keys[j]
})
var cnt int
for _, n := range keys {
cnt = count[n]
if cnt >= k {
return n
}
k -= cnt
}
return -1
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,70 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs *TreeNode
k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", &TreeNode{
Val: 5,
Left: &TreeNode{
Val: 3,
Left: &TreeNode{
Val: 5,
Left: &TreeNode{Val: 1},
Right: &TreeNode{Val: 8},
},
Right: &TreeNode{Val: 2},
},
Right: &TreeNode{
Val: 6,
Left: &TreeNode{
Val: 5,
Left: &TreeNode{Val: 6},
Right: &TreeNode{Val: 8},
},
Right: &TreeNode{Val: 7},
},
}, 2, 3},
{"TestCase1", &TreeNode{
Val: 1,
Left: &TreeNode{
Val: 2,
Left: &TreeNode{Val: 4},
Right: &TreeNode{Val: 5},
},
Right: &TreeNode{
Val: 3,
Left: &TreeNode{Val: 6},
Right: &TreeNode{Val: 7},
},
}, 1, 7},
{"TestCase3", &TreeNode{
Val: 1,
Left: &TreeNode{
Val: 2,
Right: &TreeNode{Val: 4},
},
Right: &TreeNode{Val: 3},
}, 3, -1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading