We could prove it with graph theory too. Notice from the following figure that there are only 3 types of nodes in a complete n-ary tree with N = I + L nodes, i.e., with I internal and L leaf nodes:
- 1 root node, with degree exactly n
- (I-1) internal nodes, each with degree exactly n+1 (since root is also an internal node)
- L leaf nodes, each with degree 1
Now, if we use the following two facts from graph theory:
- A tree with N nodes has N-1 edges
- Sum of the degrees of nodes in any graph is twice the number of edges in the graph
Combining all the above, we get,
Sum of degrees of all the nodes in the tree
= 1.n + (I-1)(n+1) + L.1 = 2(I+L-1)
=> L = (n-1)I + 1
=> n = (L - 1) / I + 1 = 40/10 + 1 = 5