Trace through tree [3,5,1,6,2,0,8] with p=5, q=4 (where 4 is a child of 2).
Call lca(3). Recurse left on 5 and right on 1.
Call lca(5). Node is p. Return 5 immediately.
Call lca(1). Recurse left on 0 and right on 8. Both return null. Return null.
Back at node 3: left returned 5, right returned null. Only one side found a target. But q=4 is under 5. The recursion for node 5 returned early before checking its subtrees.
That is correct. Since 5 is an ancestor of 4, and 5 is p, then 5 itself is the LCA. The algorithm returns the first target node it finds, because any ancestor of one target that is also a target is the LCA.
time, space where is the tree height.