Can you show us what you have tried? This is not a code writing site, but we would be happy to help you debug what you have written.
It sounds to me like you need to keep a state hash of attributes as you walk the tree you create and add or remove from it as you go down or up levels
-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."