Maximum number of distinct nodes in a roottoleaf pathFind the maximum number of distinct nodes in all roottoleaf paths given a Binary Tree. ExamplesExploring all root to leaf paths is a simple solution. Count distinct nodes along each roottoleaf path and return the maximum number. Hashing is an efficient solution. We traverse the tree recursively, keeping track of the number of distinct nodes on the path from the root to the current node. We repeat for the left and right subtrees, returning the maximum of two values. The implementation of the above idea is shown below. Program in C++Output 4 Time Complexity : O (n)
Next Topic#
