Since the sub-trees are numbered from 1 to N, you can identify every sub-tree root node as one of the numbers in [1, N] as the array entry nodes[i - 1].
1
N
[1, N]
nodes[i - 1]
Here's my solution.