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.
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.
- Given a binary tree, print all root-to-leaf paths
- Count of root to leaf paths in a Binary Tree that form an AP
- 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
- Find maximum GCD value from root to leaf in a Binary tree
- Find all root to leaf path sum of a Binary Tree
- Maximum value of Bitwise AND from root to leaf in a Binary tree
- Find the maximum sum leaf to root path in a 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
- Maximize count of set bits in a root to leaf 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
- Print all root to leaf paths with there relative positions
- Remove nodes on root to leaf paths of length < K
- Sum of lengths of all paths possible in a given tree
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
Improved By : SHUBHAMSINGH10