A Binary Tree is labeled if every node is assigned a label and a Binary Tree is unlabeled if nodes are not assigned any label.

Below two are considered same unlabeled trees o o / \ / \ o o o o Below two are considered different labeled trees A C / \ / \ B C A B

**How many different Unlabeled Binary Trees can be there with n nodes?**

For n = 1, there is only one tree o For n = 2, there are two trees o o / \ o o For n = 3, there are five trees o o o o o / \ / \ / \ o o o o o o / \ \ / o o o o

The idea is to consider all possible pair of counts for nodes in left and right subtrees and multiply the counts for a particular pair. Finally add results of all pairs.

For example, let T(n) be count for n nodes. T(0) = 1 [There is only 1 empty tree] T(1) = 1 T(2) = 2 T(3) = T(0)*T(2) + T(1)*T(1) + T(2)*T(0) = 1*2 + 1*1 + 2*1 = 5 T(4) = T(0)*T(3) + T(1)*T(2) + T(2)*T(1) + T(3)*T(0) = 1*5 + 1*2 + 2*1 + 5*1 = 14

The above pattern basically represents n’th Catalan Numbers. First few catalan numbers are 1 1 2 5 14 42 132 429 1430 4862,…

Here,

T(i-1) represents number of nodes on the left-sub-tree

T(n−i-1) represents number of nodes on the right-sub-tree

n’th Catalan Number can also be evaluated using direct formula.

T(n) = (2n)! / (n+1)!n!

Number of Binary Search Trees (BST) with n nodes is also same as number of unlabeled trees. The reason for this is simple, in BST also we can make any key as root, If root is i’th key in sorted order, then i-1 keys can go on one side and (n-i) keys can go on other side.

**How many labeled Binary Trees can be there with n nodes?**

To count labeled trees, we can use above count for unlabeled trees. The idea is simple, every unlabeled tree with n nodes can create n! different labeled trees by assigning different permutations of labels to all nodes.

Therefore,

Number of Labeled Tees = (Number of unlabeled trees) * n! = [(2n)! / (n+1)!n!] × n!

For example for n = 3, there are 5 * 3! = 5*6 = 30 different labeled trees

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

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:

- Generic Trees(N-array Trees)
- Count the Number of Binary Search Trees present in a Binary Tree
- Construct a Maximum Binary Tree from two given Binary Trees
- Foldable Binary Trees
- Print Common Nodes in Two Binary Search Trees
- Find all possible binary trees with given Inorder Traversal
- Check if leaf traversal of two Binary Trees is same?
- Number of full binary trees such that each node is product of its children
- Find first non matching leaves in two binary trees
- Count Balanced Binary Trees of Height h
- Merge Two Binary Trees by doing Node Sum (Recursive and Iterative)
- Number of Binary Trees for given Preorder Sequence length
- Minimum count of Full Binary Trees such that the count of leaves is N
- Iterative Approach to check if two Binary Trees are Isomorphic or not
- Total number of possible Binary Search Trees using Catalan Number
- Write Code to Determine if Two Trees are Identical
- Iterative function to check if two trees are identical
- Check if two trees are Mirror
- LCA for general or n-ary trees (Sparse Matrix DP approach )
- Iterative method to check if two trees are mirror of each other