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**.

**Example:**

Input:N = 7

Output:3

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

Output: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:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum ` `// count of trees required ` `int` `minTrees(` `int` `n) ` `{ ` ` ` ` ` `// To store the count of ` ` ` `// set bits in n ` ` ` `int` `count = 0; ` ` ` ` ` `while` `(n) { ` ` ` `n &= (n - 1); ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 7; ` ` ` ` ` `cout << minTrees(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum ` `// count of trees required ` `static` `int` `minTrees(` `int` `n) ` `{ ` ` ` ` ` `// To store the count of ` ` ` `// set bits in n ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` `n &= (n - ` `1` `); ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `7` `; ` ` ` ` ` `System.out.print(minTrees(n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum ` `# count of trees required ` `def` `minTrees(n): ` ` ` ` ` `# To store the count of ` ` ` `# set bits in n ` ` ` `count ` `=` `0` `; ` ` ` ` ` `while` `(n): ` ` ` `n &` `=` `(n ` `-` `1` `); ` ` ` `count ` `+` `=` `1` `; ` ` ` `return` `count; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `7` `; ` ` ` `print` `(minTrees(n)); ` ` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the minimum ` ` ` `// count of trees required ` ` ` `static` `int` `minTrees(` `int` `n) ` ` ` `{ ` ` ` ` ` `// To store the count of ` ` ` `// set bits in n ` ` ` `int` `count = 0; ` ` ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `n &= (n - 1); ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 7; ` ` ` ` ` `Console.Write(minTrees(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Tree with N nodes and K leaves such that distance between farthest leaves is minimized
- Find first non matching leaves in two binary trees
- Number of full binary trees such that each node is product of its children
- Minimum sum path between two leaves of a binary tree
- Extract Leaves of a Binary Tree in a Doubly Linked List
- Find the maximum path sum between two leaves of a binary tree
- Find sum of all left leaves in a given Binary Tree
- Print all nodes in a binary tree having K leaves
- Find sum of all right leaves in a given Binary Tree
- Height of binary tree considering even level leaves only
- Count of nodes which are at a distance X from root and leaves
- Find multiplication of sums of data of leaves at same levels
- Check if all leaves are at same level
- Find product of sums of data of leaves at same levels | Set 2
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
- Minimize K whose XOR with given array elements leaves array unchanged
- Count the Number of Binary Search Trees present in a Binary Tree
- Generic Trees(N-array Trees)
- Check whether a binary tree is a full binary tree or not
- Check whether a binary tree is a full binary tree or not | Iterative Approach

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.