simplified by doing a postorder tree traversal