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
This article is contributed by Sahil Chhabra (KILLER). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Given a binary tree, print all root-to-leaf paths
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Print all the paths from root, with a specified sum in Binary tree
- Find the maximum sum leaf to root path in a Binary Tree
- Print the first shortest root to leaf path in a Binary Tree
- Print root to leaf paths without using recursion
- Sum of all the numbers that are formed from root to leaf paths
- Remove nodes on root to leaf paths of length < K
- Print all root to leaf paths with there relative positions
- Root to leaf path sum equal to a given number
- Shortest root to leaf path sum equal to a given number
- Root to leaf path product equal to a given number
- GCD from root to leaf path in an N-ary tree
- Print the longest leaf to leaf path in a Binary tree
- Find if there is a pair in root to a leaf path with sum equals to root's data
Improved By : SHUBHAMSINGH10