python二叉树染色-有严重BUG

时间:2023-03-09 20:48:26
python二叉树染色-有严重BUG
#coding:utf-8
'''
二叉树涂黑
输入:
5
2 1 -1
4 2 -1
5 4 -1
3 1 1
2
输出:
3 第二题是:斗地主
'''
import sys
b=list()
class node():
def __init__(self,k=None,l=None,r=None):
self.key=k;
self.left=l;
self.right=r; def create(root,n):
n=n-1
if n==0:
return;
a=[0]*3
a=[int(i) for i in sys.stdin.readline().strip("\n").split()]
data,ff,chil=a[0],a[1],a[2] root=node(k=ff);
if chil==-1:
root.left=create(root.left,n)
if chil==1:
root.right=create(root.right,n) return root; def preorder(root): #前序遍历 print(root.key,end='');
b.append(root.key)
if root.left!=None:
preorder(root.left);
if root.right!=None:
preorder(root.right); n=int(input())
root=None;
root=create(root,n) preorder(root);
print('\n')
m=int(input())
for i in range(len(b)):
if b[i]==m:
print(len(b)-i)
break # print(type(root)) #<class '__main__.node'>
# print(root) #<__main__.node object at 0x000000000114C4E0>