Graph Theory37 sections · 1633 units
Open in Course

Full Tour Variant

(Complete node sequence)

I record every visit in the full tour, including the return steps. For a tree with edges (1,2)(1, 2), (2,4)(2, 4), and (1,3)(1, 3), the tour is [1,2,4,2,1,3,1][1, 2, 4, 2, 1, 3, 1]. This produces an array of length 2n12n-1.

You can answer lowest common ancestor (LCA) queries by taking the node with minimum depth between the first visits of uu and vv in the tour. Store the depth alongside each visit and run a range minimum query (RMQ) on that depth array. With a sparse table, preprocessing is O(nlogn)O(n \log n) time and O(nlogn)O(n \log n) space, and each query is O(1)O(1) time.