Count Nodes That Are Great Enough - Problem
Count Nodes That Are Great Enough

You are given the root of a binary tree and an integer k. Your task is to identify and count all the "great enough" nodes in the tree.

A node is considered great enough if it satisfies both of these conditions:
1. Size condition: Its subtree contains at least k nodes (including itself)
2. Value condition: Its value is greater than the values of at least k nodes in its subtree

Note that a node is considered to be in its own subtree. A subtree of node v includes v itself and all nodes that have v as an ancestor.

Goal: Return the total count of nodes that meet both criteria.

Example: If k = 2, a node needs to have at least 2 nodes in its subtree AND its value must be greater than at least 2 values in that subtree.

Input & Output

example_1.py — Basic Tree
$ Input: root = [5,3,7,1,4,null,null], k = 2
› Output: 2
šŸ’” Note: Node 5 has subtree size 5 ≄ 2 and values [1,3,4,5,7] where 3 values are smaller than 5, so 3 ≄ 2. Node 3 has subtree size 3 ≄ 2 and values [1,3,4] where 1 value is smaller than 3, but 1 < 2, so it doesn't qualify. Node 7 has subtree size 1 < 2. Only nodes 5 and 3 qualify, but node 3 fails the value condition. Actually, let me recalculate: Node 5 qualifies, Node 3 has subtree [1,3,4] with 1 value smaller than 3, so it fails. Only node 5 qualifies... Wait, this needs careful recalculation based on the exact definition.
example_2.py — Single Node
$ Input: root = [10], k = 1
› Output: 1
šŸ’” Note: Single node with value 10. Subtree has 1 node ≄ 1, and there are 0 nodes smaller than 10, but 0 < 1, so it doesn't qualify. Wait, the node counts itself in the subtree, so we need to check if 10 > itself? This needs clarification of the problem statement.
example_3.py — Large k
$ Input: root = [1,2,3], k = 5
› Output: 0
šŸ’” Note: No node has a subtree with at least 5 nodes, so no nodes can be great enough regardless of value conditions.

Visualization

Tap to expand
CEO (8)Team: 7 nodesVP (5)Team: 4VP (9)Team: 23461Great Leaders (k=2):CEO: 7 team ≄ 2, score > 5 others āœ“Not Qualified:VP(9): only 2 team, score > 1 otherBottom-up evaluation ensures accurate team metrics
Understanding the Visualization
1
Start from Leaves
Begin evaluation from bottom-level employees (leaf nodes)
2
Aggregate Reports
Each manager collects performance data from their direct reports
3
Evaluate Leadership
Check if manager meets both team size and performance ranking criteria
4
Report Upward
Pass complete team information to the next management level
Key Takeaway
šŸŽÆ Key Insight: Process tree bottom-up to avoid redundant calculations - each node gets complete subtree information from children in a single pass

Time & Space Complexity

Time Complexity
ā±ļø
O(n²)

O(n) for traversal, but collecting and processing values can be O(n) per node in worst case

n
2n
⚠ Quadratic Growth
Space Complexity
O(n²)

In worst case (skewed tree), we store O(n) values for each of O(n) nodes

n
2n
⚠ Quadratic Space

Constraints

  • 1 ≤ number of nodes ≤ 104
  • 1 ≤ Node.val ≤ 104
  • 1 ≤ k ≤ 104
  • All node values are unique
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
28.4K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen