Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2792: Count Nodes That Are Great Enough

In this guide, we solve Leetcode #2792 Count Nodes That Are Great Enough in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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 k nodes.

Quick Facts

  • Difficulty: Hard
  • Premium: Yes
  • Tags: Tree, Depth-First Search, Divide and Conquer, Binary Tree

Intuition

We need to explore a structure deeply before backing up, which suits DFS.

DFS keeps local context on the call stack and is easy to implement recursively.

Approach

Define a recursive DFS that carries the necessary state.

Combine child results as the recursion unwinds.

Steps:

  • Define a recursive DFS with state.
  • Visit children and combine results.
  • Return the final aggregation.

Example

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.

Python Solution

# 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

Complexity

The time complexity is O(V+E). The space complexity is O(V).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy