Replace items in the data tree

Dear ALL,
I would like to replace items in the data tree according to rules which I show on below printscreen:

I though that the best way is to first replace items in the data tree like:

When the end of tree branch {0; 0 } is 0, put index 0 to the data tree,

when the end of tree branch {5; 1 } is 1 - put index 1.

At the end create List Set and Replace Members.

I have problem with replacing items in data tree according to the end numbers of the each branch.

I will appreciate any help.
Replace items in data tree.gh (12.4 KB)

I’ve found the solution, if someone wants to do the same, I’m sharing.