related: #338 currently we use dfs and recursion to traverse the tree, which can be extremely un-performant. explore other options such as bfs with a queue.