Given a binary tree, print the number of root to leaf paths having equal lengths.
Input : Root of below tree 10 / 8 2 / / 3 5 2 4 Output : 4 paths are of length 3. Input : Root of below tree 10 / 8 2 / / 3 5 2 4 / 9 1 Output : 2 paths are of length 3 2 paths are of length 4
The idea is to traverse the tree and keep track of path length. Whenever we reach a leaf node, we increment path length count in a hash map.
Once we have traverse the tree, hash map has counts of distinct path lengths. Finally we print contents of hash map.
1 paths have length 4 2 paths have length 3
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.