Given an integer N and an infinite number of Full Binary Trees of different depths, the task is to choose minimum number of trees such that the sum of the count of leaf nodes in each of the tree is N.
Input: N = 7
Trees with depths 2, 1 and 0 can be picked
with the number of leaf nodes as 4, 2 and 1 respectively.
(4 + 2 + 1) = 7
Input: N = 1
Approach: Since the number of leaf nodes in a full binary tree is always a power of two. So, the problem now gets reduced to finding the powers of 2 which give N when added together such that the total number of terms in the summation is minimum which is the required answer.
Since every power of 2 contains only one ‘1’ in its binary representation, so N will contain the same number of ‘1’s as the number of terms in summation (assuming we take the minimum number of terms). So, the problem further gets reduced to finding the number of set bits in N which can be easily calculated using the approach used in this post.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count the Number of Binary Search Trees present in a Binary Tree
- Find first non matching leaves in two binary trees
- Count Balanced Binary Trees of Height h
- Count full nodes in a Binary tree (Iterative and Recursive)
- Minimum sum path between two leaves of a binary tree
- Count of nodes which are at a distance X from root and leaves
- Number of full binary trees such that each node is product of its children
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Find sum of all right leaves in a given Binary Tree
- Print all nodes in a binary tree having K leaves
- Height of binary tree considering even level leaves only
- Find sum of all left leaves in a given Binary Tree
- Tree with N nodes and K leaves such that distance between farthest leaves is minimized
- Find the maximum path sum between two leaves of a binary tree
- Extract Leaves of a Binary Tree in a Doubly Linked List
- Count even paths in Binary Tree
- Count of 1's in any path in a Binary Tree
- Count of subtrees in a Binary Tree having XOR value K
- Count all k-sum paths in a Binary 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 email@example.com. 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.