简体   繁体   English

查找 2 个树节点之间的路径

[英]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?如何打印树中任意 2 个查询节点之间的路径,而不需要二叉树? I have been using dfs and storing the paths in a vector for each query and printing it.我一直在使用 dfs 并将路径存储在每个查询的向量中并打印出来。 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. q<=10^5 太大,我的算法复杂度为 o(n q)(可能不确定)失败。n=树中没有节点。谁能帮我做一些更好的优化,以便时间复杂性降低可能是 o(n logq) 或 o(q*logn).n<=10^5。如果需要任何预计算建议我这样做。

Have you looked at using either the UCS or A* algorithms?您是否考虑过使用 UCS 或 A* 算法? A* with a good heuristic will allow you to visit the least amount of nodes具有良好启发式的 A* 将允许您访问最少数量的节点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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