条件:
array = [1,2,3,4,5,6,7,8,9,10]
# python实现二叉树的数据结果
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class BinaTree(object):
def __init__(self):
self.root = None
self.ls = []
def add(self, val):
node = TreeNode(val)
if self.root == None:
self.root = node
self.ls.append(self.root)
else:
cur_node = self.ls[0]
if cur_node.left == None:
cur_node.left = node
self.ls.append(cur_node.left)
elif cur_node.right == None:
cur_node.right = node
self.ls.append(cur_node.right)
self.ls.pop(0)
def prescan(self, root):
if root == None:
return
print(root.val)
self.prescan(root.left)
self.prescan(root.right)
if __name__ == "__main__":
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
bt = BinaTree()
for val in array:
bt.add(val)
bt.prescan(bt.root)
相关文章
暂无评论...