02. Leaf under budget
The problem can be found at the following link: Question Link
My Approach
I am using a breadth-first search (BFS) approach to traverse the tree.
I maintain a queue to keep track of nodes and their corresponding levels.
As I traverse the tree level by level, I check if a node is a leaf node (both left and right children are null).
If it is a leaf node and the level is greater than the budget (k), I return the count of leaf nodes found so far.
Otherwise, I increment the count and subtract the current level from the budget.
If the node has children, I push them into the queue with their corresponding levels.
In this solution, as we traverse from level 1 down to there leaf nodes, it naturally addresses our greedy approach to maximize the number of leaf nodes within our budget, providing the desired answers automatically.
Time and Auxiliary Space Complexity
Time Complexity: The algorithm visits each node in the tree once, so the time complexity is
O(N)
, whereN
is the number of nodes in the tree.Auxiliary Space Complexity: The space complexity is
O(W)
, whereW
is the maximum number of nodes at any level in the tree.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated