<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def nodes(self, frequency=None):
 """ Returns an iterator over all nodes in the tree for the specified frequency. If no frequency is specified then all nodes are returned Parameters frequency : str | None The frequency of the nodes to iterate over. If None all frequencies are returned Returns ------- nodes : iterable[TreeNode] """

        if frequency is None:
            nodes = chain(*(self._nodes(f)
                            for f in ('per_study', 'per_subject',
                                      'per_visit', 'per_session')))
        else:
            nodes = self._nodes(frequency=frequency)
        return nodes