我想返回二叉树中的值列表。有没有更短更有效的方法来编写数字方法?
谢谢你。
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) -> 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