Reputation: 97
Recently came across a programming question.
Given a tree, can be non-binary, can be a single chain (or linear), with N nodes.
Input will be a set of K nodes, denoted a1,a2....ak. I'd like to find the longest simple path(s) that start from one of those K nodes and finish at one of those K nodes (different from start nodes). A logarithmic algorithm depending on N or K should meet the running time requirements (eg: KlogK, KlogN) if needed should be within my desired time limit.
Thank you
Upvotes: 1
Views: 1775
Reputation: 73688
Maybe you can try this approach -
This should work for all trees, not just binary trees.
Upvotes: 3