l=[5 ,9, 8, -1, -1, 7 ,-1, -1, -1, -1, -1, 6] # l=[3, 5, 7, -1, -1, 2, 4] l=[5 ,9, 8, -1, -1, 7 ,-1, -1, -1, -1, -1, 6,3, 5, 7, -1, -1, 2, 4] # 第一步就是取出层数字典存储 dics={} for i in range(0,8): dics[str(i)]=l[0:2**i] if len(l[i:]) < 2**(i+1) : l = l[2 ** i:] if l[0:]: dics[str((i+1))] = l[0:] break l=l[2**i:] ldics=list(dics.items()) dics2={} lastleaf=ldics[-1][1] mins=max(lastleaf) lastindex=0 minleaf=[] # 计算最小叶子,倒推index for k,v in enumerate(lastleaf): if v > -1: if v <= mins: mins=v lastindex=k minleaf.append(mins) # 循环倒推index for i in range(len(ldics)-2,-1,-1): if i ==0: minleaf.append(ldics[i][1][0]) continue lastindex=ldics[i+1][1].index(ldics[i+1][1][lastindex]) lastindex += 1 if lastindex % 2 == 0: lastindex = int(lastindex / 2) lastindex -= 1 else: lastindex = int(lastindex / 2) minleaf.append(ldics[i][1][lastindex]) print(minleaf[::-1])
python 华为od 计算最小叶子路径
最新推荐文章于 2024-08-14 23:30:40 发布