写出python代码,用列表创建二叉树
时间: 2023-03-08 08:42:34 浏览: 34
基于Python实现二叉树的创建与遍历
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
# Create a binary tree from the list
def binary_tree(list):
if len(list) == 0:
return None
mid = len(list)//2
root = list[mid]
tree = {root: {'left': binary_tree(list[:mid]), 'right': binary_tree(list[mid+1:])}}
return tree
tree = binary_tree(my_list)
print(tree)
阅读全文