简体   繁体   中英

Finding path between 2 tree nodes

how can i print the path between any 2 query nodes in a tree not necessary a binary tree? I have been using dfs and storing the paths in a vector for each query and printing it. But if input query no. is too large q<=10^5,my algorithm which is of o(n q)(may be not sure) complexity fails.n=no of nodes in the tree.Can anyone help me with some better optimization so that the time complexity reduces may be o(n logq) or o(q*logn).n<=10^5.If any precomputation required suggest me so.

Have you looked at using either the UCS or A* algorithms? A* with a good heuristic will allow you to visit the least amount of nodes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM