Maximum number of distinct nodes in a root-to-leaf pathFind the maximum number of distinct nodes in all root-to-leaf paths given a Binary Tree. ExamplesExploring all root to leaf paths is a simple solution. Count distinct nodes along each root-to-leaf 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) |
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week