Count Nodes That Are Great Enough - Problem
Count Nodes That Are Great Enough
You are given the
A node is considered great enough if it satisfies both of these conditions:
1. Size condition: Its subtree contains at least
2. Value condition: Its value is greater than the values of at least
Note that a node is considered to be in its own subtree. A subtree of node
Goal: Return the total count of nodes that meet both criteria.
Example: If
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 subtreeNote 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
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
ā Quadratic Growth
Space Complexity
O(n²)
In worst case (skewed tree), we store O(n) values for each of O(n) nodes
ā Quadratic Space
Constraints
- 1 ⤠number of nodes ⤠104
- 1 ⤠Node.val ⤠104
- 1 ⤠k ⤠104
- All node values are unique
š”
Explanation
AI Ready
š” Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code