Maximum number of leaf nodes that can be visited within the given budget
Given a binary tree and an integer b representing budget. The task is to find the count of maximum number of leaf nodes that can be visited under the given budget if cost of visiting a leaf node is equal to level of that leaf node.
Note: Root of the tree is at level 1.
Examples:
Input: b = 8
10
/ \
8 15
/ / \
3 11 18
\
13
Output: 2
For the above binary tree, leaf nodes are 3,
13 and 18 at levels 3, 4 and 3 respectively.
Cost for visiting leaf node 3 is 3
Cost for visiting leaf node 13 is 4
Cost for visiting leaf node 18 is 3
Thus with given budget = 8, we can at maximum
visit two leaf nodes.
Input: b = 1
8
/ \
7 10
/
3
Output: 0
For the above binary tree, leaf nodes are
3 and 10 at levels 3 and 2 respectively.
Cost for visiting leaf node 3 is 3
Cost for visiting leaf node 10 is 2
In given budget = 1, we can't visit
any leaf node.
Approach:
- Traverse the binary tree using level order traversal, and store the level of all the leaf nodes in a priority queue.
- Remove an element from the priority queue one by one, check if this value is within the budget.
- If Yes then subtract this value from the budget and update count = count + 1.
- Else, print the count of maximum leaf nodes that can be visited within the given budget.
Below is the implementation of the above approach:
// Java program to calculate the maximum number of leaf // nodes that can be visited within the given budget import java.io.*; import java.util.*; import java.lang.*; // Class that represents a node of the tree class Node { int data; Node left, right; // Constructor to create a new tree node Node(int key) { data = key; left = right = null; } } class GFG { // Priority queue to store the levels // of all the leaf nodes static PriorityQueue<Integer> pq; // Level order traversal of the binary tree static void levelOrder(Node root) { Queue<Node> q = new LinkedList<>(); int len, level = 0; Node temp; // If tree is empty if (root == null) return; q.add(root); while (true) { len = q.size(); if (len == 0) break; level++; while (len > 0) { temp = q.remove(); // If left child exists if (temp.left != null) q.add(temp.left); // If right child exists if (temp.right != null) q.add(temp.right); // If node is a leaf node if (temp.left == null && temp.right == null) pq.add(level); len--; } } } // Function to calculate the maximum number of leaf nodes // that can be visited within the given budget static int countLeafNodes(Node root, int budget) { pq = new PriorityQueue<>(); levelOrder(root); int val; // Variable to store the count of // number of leaf nodes possible to visit // within the given budget int count = 0; while (pq.size() != 0) { // Removing element from // min priority queue one by one val = pq.poll(); // If current val is under budget, the // node can be visited // Update the budget afterwards if (val <= budget) { count++; budget -= val; } else break; } return count; } // Driver code public static void main(String args[]) { Node root = new Node(10); root.left = new Node(8); root.right = new Node(15); root.left.left = new Node(3); root.left.left.right = new Node(13); root.right.left = new Node(11); root.right.right = new Node(18); int budget = 8; System.out.println(countLeafNodes(root, budget)); } } |
chevron_right
filter_none
Output:
2
Recommended Posts:
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Maximum sum of leaf nodes among all levels of the given binary tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Root to leaf path with maximum distinct nodes
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Delete leaf nodes with value k
- Delete leaf nodes with value as x
- Level with maximum number of nodes
- Count distinct points visited on the number line
- Print all leaf nodes of an n-ary tree using DFS
- Sum of leaf nodes at minimum level
- Print Leaf Nodes at a given Level
- Sum of all leaf nodes of binary tree
- Level with maximum number of nodes using DFS in a N-ary tree
- Count Non-Leaf nodes in a Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



