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 firstname.lastname@example.org. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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.
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Print all the paths from root, with a specified sum in Binary tree
- Boundary Root to Leaf Path traversal of a Binary Tree
- Print the longest path from root to leaf in a Binary tree
- Print the first shortest root to leaf path in a Binary Tree
- Find the maximum sum leaf to root path in a Binary Tree
- Sum of all the numbers that are formed from root to leaf paths
- Print root to leaf paths without using recursion
- Remove nodes on root to leaf paths of length < K
- Print all root to leaf paths with there relative positions
- Sum of lengths of all paths possible in a given tree
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- 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
Improved By : SHUBHAMSINGH10