Might there be a simpler way to write this code? I'd like to learn to write code more efficiently.
def leaves_and_internals(self):
"""(BTNode) -> ([object], [object])
Return a pair of lists: (list of all the items stored in the leaves of
the subtree rooted at this node, list of all the items stored in the
internal nodes of the subtree rooted at this node).
"""
leaves = []
internal = []
if self.left is None and self.right is None:
leaves.append(self.item)
else:
internal.append(self.item)
if self.left:
subleaf, subinternal = self.left.leaves_and_internals()
leaves.extend(subleaf)
internal.extend(subinternal)
if self.right:
subleaf, subinternal = self.right.leaves_and_internals()
leaves.extend(subleaf)
internal.extend(subinternal)
return (leaves, internal)