Conditional nesting in recursive query

Hello,

How to make a recursive query with conditional nesting?

For ex.:

I have a parent node A with array fields A.PathA and A.PathB. Each field is referenced to other different nodes:
A.PathA: [A_A_1, …, A_A_n]
A.PathB: [A_B_1, …, A_B_n]

Other nodes also have their own array fields like above, for ex:
A_A_1.PathA: [A_A_A_1, …, A_A_A_n]
A_B_1.PathB: [A_B_B_1, …, A_B_B_n]

Etc.

So, I have a start node with some UID, for ex. node A.

I want query to traverse through:

A.PathA → A_A_1.PathB → … → Node_N.PathA → Node_N+1.PathB
or
A.PathB → A_B_1.PathA → … → Node_N.PathB → Node_N+1.PathA

Thus, if the query comes from PathA, it should go through PathB.

Thanks in advance.

Best regards,
Nikolai