The Density of a Binary Tree Algorithm

The density of a binary tree is defined as the ratio of the tree’s size to the tree’s height.

 The height of the tree is two, and the size of the tree is 3. Hence the ratio is 1.5

Approach 1

The approach one is simple and quite obvious. Calculate the size of the tree and the height of the tree separately. After that, calculate the ratio.

C++ code:

Output:

Pin It on Pinterest

Share This