To decide which of or is deeper during path queries, precompute depth[v] for each node. The root has depth[root] = 0, and depth[child] = depth[parent] + 1. During path queries, always move the node with greater depth.
This ensures you do not overshoot the LCA. Both nodes gradually move up until they meet. Computing depths takes time in a single DFS. You can compute depth, parent, and subtree size in the same DFS to save time.
Space complexity is for the data structures used.