defget_track(i,D):
ans,i,pos = '',i-1,1for d in range(0,D-1):
k = i>>d
if k%2:pos = pos*2+1else:pos*=2
print('*****:',pos)
defproc():
num = int(input())
while num>0:
line = input()
line = line.split(' ')
D,I = int(line[0]),int(line[1])
get_track(I,D)
num-=1
proc()
上面的思路相对麻烦一点,书上提供了一种更加简单的方法,这种方法有点递归的思想:
对于第i个小球:,
if i%2==1那么它就是第(i+1)/2个向左的小球,否则就是第i/2个向右的小球。那么该层的行为就可以确定。同时,对于左/右子树,i = next(i),继续这个判断既可。相对于我自己的办法,这种思路逻辑更加清晰,非常巧妙的解决了问题!代码如下:
defget_track(i,D):
ans,pos = '',1for d in range(0,D-1):
if i%2:pos,i = pos*2,(i+1)>>1else:pos,i=2*pos+1,i>>1
defproc_input():
line = input()
while line!='':
nodes = []
line = line.split(' ')
for node_expr in line:
if node_expr=='()':continue
node_expr = node_expr[1:len(node_expr)-1]
node_expr = node_expr.split(',')
node = (node_expr[0],node_expr[1])
nodes.append(node)
nodes.sort(key = lambda x: (len(x[1]),x[1]))
if test(nodes):print([i[0] for i in nodes])
else:print(-1)
line = input()
deftest(nodes):
root = nodes[0] if nodes[0][1]==''elseNoneif root==None:print('no root');returnFalse
tree = [root,None,None]
curr,parent = tree,root
for node in nodes[1:]:
curr = tree
for way in node[1]:
if curr==None:break
parent = curr
if way=='L':curr = curr[1]
else:curr = curr[2]
if curr!=None:returnFalse#repeatif parent[0][1]==node[1][:-1]:
if way=='L':parent[1] = [node,None,None]
else: parent[2] = [node,None,None]
else:print('len:',parent[0],node);returnFalsereturnTrue
proc_input()
补充,还可以使用数组来表达二叉树,代码如下:
注意,下面的代码只针对上面的题目。
cnt,root = 1,1
left,right,halv_val = [0,0],[0,0],[0,0]
defnewtree():
left[root],right[root],halv_val[root] = 0,0,Falsedefnewnode():global cnt,u
u,cnt = cnt+1,cnt+1
left.append(0);right.append(0);halv_val.append(False)
return u
defaddnode(node):global cnt,root
u,way = 1,node[1]
for d in way:
if d=='':halv_val[root] = node
elif d=='L':
if left[u]==0:left[u]=newnode()
u = left[u]
else:
if right[u]==0:right[u]=newnode()
u = right[u]
if halv_val[u]==True:assert(1<0)
halv_val.append(node)
newtree()
print(halv_val,left,right)
def create_mobile(preorder):
wl,dl,wr,dr = preorder.pop() b1,b2 = True,True if wl==0:b1,wl = create_mobile(preorder) if wr==0:b2,wr = create_mobile(preorder) return (b1 and b2 and wl*dl==wr*dr),wl+wr
print(create_mobile([[0,2,0,4],[0,3,0,1],[1,1,1,1],[2,4,4,2],[1,6,3,2]][::-1]))
6−10下落的叶子,和6-9非常的相似,我这里也略去对输入的处理了。
defcompute(preorder):
value = {}
deffall_tree(preorder,horizon):
root = preorder.pop()
value[horizon] = root+value[horizon] if horizon in value else root
if preorder[-1]!=-1:fall_tree(preorder,horizon-1)
else:preorder.pop()
if preorder[-1]!=-1:fall_tree(preorder,horizon+1)
else:preorder.pop()
return
fall_tree(preorder[::-1],0)
print([value[k] for k in sorted(value.keys())])
compute([5,7,-1,6,-1,-1,3,-1,-1])
compute([8,2,9,-1,-1,6,5,-1,-1,12,-1,-1,3,7,-1,-1,-1])
defproc(p1,p2):
sum = 0defremov(p):for i in range(4):p.pop()
defcompute(order,num):nonlocal sum
root = order.pop()
if root=='f':sum+=num
elif root == 'p':
for i in range(4):compute(order,num>>2)
defquadtrees(preoder1,preoder2,num):nonlocal sum
root1,root2 = preoder1.pop(),preoder2.pop()
if root1=='f'or root2=='f':
sum+=num
if root1=='p':remov(preoder1)
if root2=='p':remov(preoder2)
elif root1 == 'e'and root2=='e':returnelif root1=='p'and root2=='p':
for i in range(4):quadtrees(preoder1,preoder2,num>>2);
elif root1=='p':preoder1.append('p');compute(preoder1,num)
else: preoder2.append('p');compute(preoder2,num)
quadtrees(p1[::-1],p2[::-1],1024)
print(sum)
proc(list('ppeeefpffeefe'),list('pefepeefe'))
proc(list('peeef'),list('peefe'))
proc(list('peeef'),list('peepefefe'))