diff --git a/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/1.png b/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/1.png new file mode 100644 index 000000000..5da539460 Binary files /dev/null and b/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/1.png differ diff --git a/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/2.png b/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/2.png new file mode 100644 index 000000000..8f38eba60 Binary files /dev/null and b/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/2.png differ diff --git a/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/3.png b/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/3.png new file mode 100644 index 000000000..264055011 Binary files /dev/null and b/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/3.png differ diff --git a/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/README.md b/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/README.md index 9bbdbc933..0ab429712 100755 --- a/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/README.md +++ b/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/Solution.go b/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/Solution.go index d115ccf5e..2b6668c23 100644 --- a/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/Solution.go +++ b/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/Solution.go @@ -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 } diff --git a/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/Solution_test.go b/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/Solution_test.go index 14ff50eb4..c64b4617e 100644 --- a/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/Solution_test.go +++ b/leetcode/3301-3400/3319.K-th-Largest-Perfect-Subtree-Size-in-Binary-Tree/Solution_test.go @@ -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() { }