X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Leaf under budget
Easy Accuracy: 38.93% Submissions: 5232 Points: 2

Given a binary tree and a budget. Assume you are at the root of the tree(level 1), you need to maximise the count of leaf nodes you can visit in your budget if the cost of visiting a leaf node is equal to the level of that leaf node.

Example 1:

Input: 
                  10
                /    \
               8      2
             /      /   \
            3      3     6
                    \
                     4
and budget = 8
Output: 2
Explanation:
Cost For visiting Leaf Node 3: 3
Cost For visiting Leaf Node 4: 4
Cost For visiting Leaf Node 6: 3
In budget 8 one can visit Max 2 Leaf Nodes.

Example 2:

Input: 
            1
       /   \
      2     3
     / \   / \
    4   5 6   7
and budget = 5
Output: 1

Your Task:

You don't need to read input or print anything. Your task is to complete the function getCount() which takes root node of the tree and a integer denoting the budget as input parameters and returns an integer denoting the count of visited leaf nodes of the tree.


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1<=T<=103
1<=N<=104
1<=budget<=104

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Leaf under budget

Output Window