Implemented depth-first traversal.