[Profiler] Fix traversal utility (#85717)
`eventTreeDFS` traverses in the wrong order (right to left). Moreover, we will need more complex traversal (e.g. early stopping) for memory profiler. Thus, I made a simple general `_traverse` method and added `functools.partial` specializations for DFS and BFS.
Differential Revision: [D39788871](https://our.internmc.facebook.com/intern/diff/D39788871/)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/85717
Approved by: https://github.com/chaekit