I want to return a list of the values from the binary tree. Is there a shorter and more efficient way to write the method for numbers?
class BTNode(object):
"""A node in a binary tree."""
def __init__(self, item, left=None, right=None):
"""(BTNode, object, BTNode, BTNode) -> NoneType
Initialize this node to store item and have children left and right,
as well as depth 0.
"""
self.item = item
self.left = left
self.right = right
self.depth = 0 # the depth of this node in a tree
def number(self: 'BTNode') -> list:
lst = []
if self.right is None and self.left is None:
lst.append(self.item)
else:
lst.append(self.item)
if self.left:
left = self.left.number()
lst.extend(left)
if self.right:
right = self.right.number()
lst.extend(right)
return lst
number
would be better namedenumerate
. – 200_success♦ Dec 28 '13 at 18:56