Count Nodes That Are Great Enough Problem
Description
LeetCode Problem 2792.
You are given a root to a binary tree and an integer k. A node of this tree is called great enough if the followings hold:
- Its subtree has at least
knodes. - Its value is greater than the value of at least
knodes in its subtree.
Return the number of nodes in this tree that are great enough.
The node u is in the subtree of the node v, if u == v or v is an ancestor of u.
Example 1:
Input: root = [7,6,5,4,3,2,1], k = 2
Output: 3
Explanation: Number the nodes from 1 to 7.
The values in the subtree of node 1: {1,2,3,4,5,6,7}. Since node.val == 7, there are 6 nodes having a smaller value than its value. So it's great enough.
The values in the subtree of node 2: {3,4,6}. Since node.val == 6, there are 2 nodes having a smaller value than its value. So it's great enough.
The values in the subtree of node 3: {1,2,5}. Since node.val == 5, there are 2 nodes having a smaller value than its value. So it's great enough.
It can be shown that other nodes are not great enough.
See the picture below for a better understanding.

Example 2:
Input: root = [1,2,3], k = 1
Output: 0
Explanation: Number the nodes from 1 to 3.
The values in the subtree of node 1: {1,2,3}. Since node.val == 1, there are no nodes having a smaller value than its value. So it's not great enough.
The values in the subtree of node 2: {2}. Since node.val == 2, there are no nodes having a smaller value than its value. So it's not great enough.
The values in the subtree of node 3: {3}. Since node.val == 3, there are no nodes having a smaller value than its value. So it's not great enough.
See the picture below for a better understanding.

Example 3:
Input: root = [3,2,2], k = 2
Output: 1
Explanation: Number the nodes from 1 to 3.
The values in the subtree of node 1: {2,2,3}. Since node.val == 3, there are 2 nodes having a smaller value than its value. So it's great enough.
The values in the subtree of node 2: {2}. Since node.val == 2, there are no nodes having a smaller value than its value. So it's not great enough.
The values in the subtree of node 3: {2}. Since node.val == 2, there are no nodes having a smaller value than its value. So it's not great enough.
See the picture below for a better understanding.

Constraints:
- The number of nodes in the tree is in the range
[1, 10^4]. 1 <= Node.val <= 10^41 <= k <= 10
Solution
Approach: Post-order DFS + Max-Heap of Size k
A node is great enough when its subtree has at least k values strictly smaller than its own. For each subtree we only need its k smallest values. Do a post-order DFS that returns a max-heap holding at most k values: merge the right child's values into the left child's heap, dropping the largest whenever the size exceeds k. If the heap is full and its largest value (the k-th smallest) is less than the node's value, count the node. Then insert the node's own value, trimming to k again, and return the heap.
Complexity: O(n * k * log k) time and O(n + k) space, where n is the number of nodes.
from heapq import heappop, heappush
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def countGreatEnoughNodes(self, root: Optional[TreeNode], k: int) -> int:
def push(pq, x):
heappush(pq, x)
if len(pq) > k:
heappop(pq)
def dfs(root):
if root is None:
return []
l, r = dfs(root.left), dfs(root.right)
for x in r:
push(l, x)
if len(l) == k and -l[0] < root.val:
nonlocal ans
ans += 1
push(l, -root.val)
return l
ans = 0
dfs(root)
return ans