写出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

猜你喜欢

转载自blog.csdn.net/weixin_42599558/article/details/129488137