recursive tree builder