Depth-first search for atoms in molecule