ZhenlongDai/ACPR huggingface dataset:
Problem: p02665
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N = int(input())
A = list(map(int, input().split()))
bottom = sum(A)

if A[0] >= 2:
  print(-1)
else:
  print(1)

exit()
ret = 1
children = 1 - A[0]
bottom -= A[0]
 
for i in range(N):
    children = children * 2 - A[i+1]
    if children <= -1:
      ret = -1
      break
    
    bottom -= A[i+1]
    if children >= bottom:
      children = bottom
    ret += children + A[i+1]
print(ret)


Failing test case input:
4
0 1 -3 2 14

Expected output:
29


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
A = list(map(int, input().split()))
bottom = sum(A)

if A[0] >= 2:
  print(-1)
  exit()

ret = 1
children = 1 - A[0]
bottom -= A[0]
 
for i in range(N):
    children = children * 2 - A[i+1]
    if children <= -1:
      ret = -1
      break
    
    bottom -= A[i+1]
    if children >= bottom:
      children = bottom
    ret += children + A[i+1]
print(ret)
Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
iN = int(input())
aS = [int(x) for x in input().rstrip().split()]







iCounter = 0
if iN == 2:
    if aS[0] == aS[1]:
        iCounter += 1
else:
    for i in range(1,iN-1):
        if aS[i] == aS[i-1]:
            if aS[i] == aS[i+1]:
                if aS[i] < 10000:
                    aS[i] += 1
                else:
                    aS[i] -= 1
                iCounter += 1
            else:
                iU = max(aS[i],aS[i+1])
                if iU < 10000:
                    iU += 1
                else:
                    iU -= 1
                    if iU == min(aS[1],aS[i+1]):
                        iU -= 1
                aS[i] = iU
                iCounter += 1
print(iCounter)


Failing test case input:
5
1 2 9 2 2

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
iN = int(input())
aS = [int(x) for x in input().rstrip().split()]







iCounter = 0
if iN == 2:
    if aS[0] == aS[1]:
        iCounter += 1
else:
    for i in range(1,iN-1):
        if aS[i] == aS[i-1]:
            if aS[i] == aS[i+1]:
                if aS[i] < 10000:
                    aS[i] += 1
                else:
                    aS[i] -= 1
                iCounter += 1
            else:
                iU = max(aS[i],aS[i+1])
                if iU < 10000:
                    iU += 1
                else:
                    iU -= 1
                    if iU == min(aS[1],aS[i+1]):
                        iU -= 1
                aS[i] = iU
                iCounter += 1
    if aS[iN-2] == aS[iN-1] :
        iCounter += 1
print(iCounter)

Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
a = list(map(int, input().split()))
ans = 0
count = 1
for i in range(1, len(a)):
	if a[i] == a[i-1]:
		count += 1
		if i == len(a) - 1 and count > 2:
			ans += int(count / 2)
		elif i == len(a) - 1 and count == 2:
			ans += 1
	elif a[i] != a[i-1] and count == 2:
		ans += 1
		count = 1
	elif a[i] != a[i-1] and count > 2:
		ans += count - 2
		count = 1
print(ans)

Failing test case input:
14
1 1 3 3 3 4 4 4 4 4 1 2 3 4

Expected output:
4


Actual output:
5


Fixed submission (verdict: Accepted):
N = int(input())
a = list(map(int, input().split()))
ans = 0
count = 1
for i in range(1, len(a)):
	if a[i] == a[i-1]:
		count += 1
		if i == len(a) - 1 and count > 2:
			ans += int(count / 2)
		elif i == len(a) - 1 and count == 2:
			ans += 1
	elif a[i] != a[i-1] and count == 2:
		ans += 1
		count = 1
	elif a[i] != a[i-1] and count > 2:
		ans += int(count / 2)
		count = 1
print(ans)

Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
A = list(map(int,input().split()))
ans = 0
K = max(A)+1
for i in range(N-2):
    if A[i] == A[i+1]:
        A[i+1] = K
        K += 1
        ans += 1
print(ans)

Failing test case input:
5
8 3 9 2 2

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
N = int(input())
a = list(map(int,input().split()))
ans = 0
P = max(a)
for i in range(N-1):
  if a[i+1] == a[i]:
    P += 1
    a[i+1] = P
    ans += 1
print(ans)
Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = input()
data = input().split()
bef = -1
count = 0
result = 0

for i in range(len(data)):
	t = int(data[i])
	if bef != t:
		if count > 1:
			result += int(count) / 2
			print(result)
		count = 1
		bef = t
	else:
		count += 1

if count > 1:
	result += int(count / 2)
print(result)

Failing test case input:
5
1 2 1 1 0

Expected output:
1


Actual output:
1.0
1.0


Fixed submission (verdict: Accepted):
N = input()
data = input().split()
bef = -1
count = 0
result = 0

for i in range(len(data)):
	t = int(data[i])
	if bef != t:
		if count > 1:
			result += int(count / 2)
		count = 1
		bef = t
	else:
		count += 1

if count > 1:
	result += int(count / 2)
print(result)

Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from sys import stdin


def get(l, r):
    for i in range(1, 10001):
        if i != l and i != r:
            return i
    assert False, 'damepo'


def main():
    N = int(stdin.readline().rstrip())
    a = [int(x) for x in stdin.readline().rstrip().split()]
    l = a[0]
    count = 0
    for i, x in enumerate(a[1:-1]):
        idx = i + 1
        l = a[idx - 1]
        r = a[idx + 1]
        if l != x != r:
            pass
        elif (idx + 3) < len(a):
            if x == r == a[idx + 2] and x != a[idx + 3]:
                continue
        elif (idx + 2) < len(a):
            if x == r == a[idx + 2]:
                continue
        x = get(l, r)
        count += 1
        a[idx] = x
    
    print(count)


if __name__ == '__main__':
    main()


Failing test case input:
5
3 1 12 3 4

Expected output:
0


Actual output:
3


Fixed submission (verdict: Accepted):
from sys import stdin


def get(l, r):
    for i in range(1, 10001):
        if i != l and i != r:
            return i
    assert False, 'damepo'


def main():
    N = int(stdin.readline().rstrip())
    a = [int(x) for x in stdin.readline().rstrip().split()]

    count = 0
    if N == 2:
        if a[0] == a[1]:
            count += 1
    else:
        for i, x in enumerate(a[1:-1]):
            idx = i + 1
            l = a[idx - 1]
            r = a[idx + 1]
            if (l == x and x != r) or (l == x == r) or ((idx == len(a) - 2) and x == r):
                x = get(l, r)
                count += 1
                a[idx] = x
        
    print(count)


if __name__ == '__main__':
    main()

Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
a=[]
a=input().split()
sum=0

for i in range (1,N-2):
  if a[i-1]==a[i] or a[i+1]==a[i]:
    i=i+1
    sum=sum+1

print(sum)

Failing test case input:
14
1 1 3 2 3 4 3 4 4 4 1 2 3 4

Expected output:
2


Actual output:
4


Fixed submission (verdict: Accepted):
N=int(input())
a=[]
a=input().split()
sum=0
 
for i in range (1,N):
  if a[i-1]==a[i]:
    a[i]=i+10001
    sum=sum+1
 
print(sum)
Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
a =int(input())
b =list(map(int,input().split()))
print(a)
print(b)
x=0
for i in range(a-1):
	if b[i]==b[i+1]:
		b[i+1]=11111
		x=x+1
print(x)

Failing test case input:
14
1 1 2 3 3 3 4 4 4 4 1 2 3 4

Expected output:
4


Actual output:
14
[1, 1, 2, 3, 3, 3, 4, 4, 4, 4, 1, 2, 3, 4]
4


Fixed submission (verdict: Accepted):
a =int(input())
b =list(map(int,input().split()))
x=0
for i in range(a-1):
	if b[i]==b[i+1]:
		b[i+1]=11111
		x=x+1
print(x)

Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n-2):
    if a[i]==a[i+1]:
        a[i+1]=-1
        ans+=1
print(ans)

Failing test case input:
5
15 5 12 3 3

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n-1):
    if a[i]==a[i+1]:
        a[i+1]=-1
        ans+=1
print(ans)

Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
a = [int(x) for x in input().split()]
count = 0
if (N==len(a))and(2<=N):
    for i in range(1,N-1):
        if (a[i-1]==a[i]):
            a[i] += 1
            if a[i] > 10000:
                a[i] = 1
            if a[i]==a[i+1]:
                a[i] += 1
                if a[i] > 10000:
                    a[i] = 1
            count += 1
    print(count)

Failing test case input:
5
4 2 9 2 2

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
N = int(input())
a = [int(x) for x in input().split()]
count = 0
a.append(0)
if (N+1==len(a))and(2<=N):
    for i in range(1,N):
        if (a[i-1]==a[i]):
            a[i] += 1
            if a[i] > 10000:
                a[i] = 1
            if a[i]==a[i+1]:
                a[i] += 1
                if a[i] > 10000:
                    a[i] = 1
            count += 1
    print(count)

Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
a_tmp = input().split()
a = []
ans = 0
for _ in a_tmp:
    a.append(int(_))
i = 0
same = 1
while i < n-1:
    tmp = a[i]
    i += 1
    if a[i] == tmp:
        same += 1
        if same == 3:
            ans += 1
            same = 1
    else:
        if same >= 2:
            ans += 1
            same = 1
print(ans)


Failing test case input:
5
1 1 3 2 2

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())
a_tmp = input().split()
a = []
ans = 0
for _ in a_tmp:
    a.append(int(_))
i = 0
same = 1
while i < n-1:
    tmp = a[i]
    i += 1
    if a[i] == tmp:
        same += 1
        if same == 3:
            ans += 1
            same = 1
    else:
        if same >= 2:
            ans += 1
            same = 1
if same >= 2:
    ans += 1

print(ans)
Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
a=[int(i)for i in input().split()]

count=0
p=1
while p<n-1:
    if a[p-1] == a[p]:
        if a[p-1] == a[p] == a[p+1]:
            a[p]=a[p-1]+a[p]+a[p+1]
            p+=2
            count+=1
        else:
            a[p]=a[p-1]+a[p]+a[p+1]
            p+=2
            count+=1
    else:
        p+=1
print(count)


Failing test case input:
3
1 2 2

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n=int(input())
a=[int(i)for i in input().split()]

count=0
p=1
while p<n:
    if a[p-1] == a[p]:
        try:
         if a[p-1] == a[p] == a[p+1]:
             a[p]=a[p-1]+a[p]+a[p+1]
             p+=1
             count+=1
         else:
             a[p]=a[p-1]+a[p]+a[p+1]
             p+=1
             count+=1
        except:
            if a[p-1]==a[p]:
                p+=1
                count+=1
    p+=1
print(count)

Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
A = list(map(int, input().split()))

ans = 0
for i in range(1, N - 1):
    if A[i - 1] == A[i]:
        ans += 1
        A[i] = max(A[i - 1], A[i + 1]) + 1

print(ans)

Failing test case input:
5
1 1 9 2 2

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
A = list(map(int, input().split()))

ans = 0
pr = -1
for i in range(1, N):
    if A[i - 1] == A[i]:
        ans += 1
        A[i] = pr
        pr -= 1

print(ans)

Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def main():
    
    N = int(input())
    *A, = map(int, input().split())

    
    ans = 0
    i = 0
    while i < N:
        if A[i-1] == A[i]:
            ans += 1
            i += 2
        else:
            i += 1
    print(ans)


if __name__ == '__main__':
    main()


Failing test case input:
5
2 1 1 3 2

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
def main():
    
    N = int(input())
    *A, = map(int, input().split())

    
    ans = 0
    i = 1
    while i < N:
        if A[i-1] == A[i]:
            ans += 1
            i += 2
        else:
            i += 1
    print(ans)


if __name__ == '__main__':
    main()
Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
iN = int(input())
aS = [int(x) for x in input().rstrip().split()]



iCounter = 0
if iN == 2:
    if aS[0] == aS[1]:
        iCounter += 1
else:
    for i in range(1,iN-1):
        if aS[i] == aS[i-1]:
            if aS[i] == aS[i+1]:
                if aS[i] != 10000:
                    aS[i] += 1
                else:
                    aS[i] -= 1
                iCounter += 1
            else:
                if i == iN - 2:
                    continue
                else:
                    iU = max(aS[i],aS[i+1])
                    if iU < 10000:
                        iU += 1
                    else:
                        iU -= 1
                        if iU == min(aS[1],aS[i+1]):
                            iU -= 1
                    aS[i] = iU
                    iCounter += 1
print(iCounter)


Failing test case input:
5
1 0 1 1 0

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
iN = int(input())
aS = [int(x) for x in input().rstrip().split()]







iCounter = 0
if iN == 2:
    if aS[0] == aS[1]:
        iCounter += 1
else:
    for i in range(1,iN-1):
        if aS[i] == aS[i-1]:
            if aS[i] == aS[i+1]:
                if aS[i] < 10000:
                    aS[i] += 1
                else:
                    aS[i] -= 1
                iCounter += 1
            else:
                iU = max(aS[i],aS[i+1])
                if iU < 10000:
                    iU += 1
                else:
                    iU -= 1
                    if iU == min(aS[1],aS[i+1]):
                        iU -= 1
                aS[i] = iU
                iCounter += 1
    if aS[iN-2] == aS[iN-1] :
        iCounter += 1
print(iCounter)

Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import random
N = int(input())
a = [int(ai) for ai in input().split()]

count = 0
for i in range(0, N-1):
    if a[i] == a[i+1]:
        count += 1
    while a[i] == a[i+1]:
        a[i+1] = random.randint(1, N)
print(count)

Failing test case input:
5
1 1 2 2 2

Expected output:
2

Actual output:
3


Fixed submission (verdict: Accepted):
import random
N = int(input())
a = [int(ai) for ai in input().split()]

random.seed(42)
count = 0
for i in range(0, N-1):
    if a[i] == a[i+1]:
        count += 1
    while a[i] == a[i+1]:
        a[i+1] = random.randint(1, N)
        if i < N - 2:
            while a[i+1] == a[i+2]:
                a[i+1] = random.randint(1, N)

print(count)

Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(N-1):
    if a[i]==a[i+1]:
        ans+=1
print(ans)

Failing test case input:
14
1 2 2 3 3 3 4 4 4 4 1 2 3 4

Expected output:
4

Actual output:
6


Fixed submission (verdict: Accepted):
N=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(N-1):
    if a[i]==a[i+1]:
        a[i+1]=10**4+i+1
        ans+=1
print(ans)
Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
a = input().split()
count = 0
for i in range(n - 1):
	if a[i] == a[i + 1]:
		if i + 1 < n - 2:
			if a[i] != 1 and a[i + 2] != 1:
				a[i + 1] = 1
				count += 1
			elif a[i] != 2 and a[i + 2] != 2:
				a[i + 1] = 2
				count += 1
			elif a[i] != 3 and a[i + 2] != 3:
				a[i + 1] = 3
				count += 1
		if i == n - 2:
			if a[i] == 1:
				a[i + 1] = 2
				count += 1
				flag = 1
			else:
				a[i + 1] = 1
				count += 1
				flag = 1

print(count)

Failing test case input:
5
2 0 1 1 0

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n = int(input())
a = input().split()
count = 0
for i in range(n - 1):
	if a[i] == a[i + 1]:
		if i + 1 < n - 1:
			if a[i] != 1 and a[i + 2] != 1:
				a[i + 1] = 1
				count += 1
			elif a[i] != 2 and a[i + 2] != 2:
				a[i + 1] = 2
				count += 1
			elif a[i] != 3 and a[i + 2] != 3:
				a[i + 1] = 3
				count += 1

		if i == n - 2:
			if a[i] == 1:
				a[i + 1] = 2
				count += 1
			else:
				a[i + 1] = 1
				count += 1

print(count)

Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
a=[]
a=input().split()
sum=0

for i in range (1,N-1):
  if a[i-1]==a[i]:
    a[i]=i+10000
    sum=sum+1

print(sum)

Failing test case input:
5
15 5 12 3 3

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
N=int(input())
a=[]
a=input().split()
sum=0
 
for i in range (1,N):
  if a[i-1]==a[i]:
    a[i]=i+10001
    sum=sum+1
 
print(sum)

Problem: p03296
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n = int(input())
a = list(map(int,input().split()))
ans = 0
befor = a[0]
for i in range(1,n-1):
    after = a[i+1]
    if a[i] == befor:
        ans += 1
        for j in range(1,10001):
            if j != after:
                a[i] = j
                break
    befor = a[i]

print(ans)


Failing test case input:
5
8 3 9 2 2

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n = int(input())
a = list(map(int,input().split()))
ans = 0
befor = a[0]
for i in range(1,n-1):
    after = a[i+1]
    if a[i] == befor:
        ans += 1
        for j in range(1,10001):
            if j != after:
                a[i] = j
                break
    befor = a[i]
if befor == a[-1]:
    ans += 1
print(ans)
Problem: p03296
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
a = input().split()
count = 0
for i in range(n - 1):
	if a[i] == a[i + 1]:
		if i + 1 < n - 2:
			if a[i] != 1 and a[i + 2] != 1:
				a[i + 1] = 1
				count += 1
			elif a[i] != 2 and a[i + 2] != 2:
				a[i + 1] = 2
				count += 1
			elif a[i] != 3 and a[i + 2] != 3:
				a[i + 1] = 3
				count += 1

		if i == n - 2:
			if a[i] == 1:
				a[i + 1] = 2
				count += 1
			else:
				a[i + 1] = 1
				count += 1

print(count)

Failing test case input:
5
2 0 1 1 0

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
n = int(input())
a = input().split()
count = 0
for i in range(n - 1):
	if a[i] == a[i + 1]:
		if i + 1 < n - 1:
			if a[i] != 1 and a[i + 2] != 1:
				a[i + 1] = 1
				count += 1
			elif a[i] != 2 and a[i + 2] != 2:
				a[i + 1] = 2
				count += 1
			elif a[i] != 3 and a[i + 2] != 3:
				a[i + 1] = 3
				count += 1

		if i == n - 2:
			if a[i] == 1:
				a[i + 1] = 2
				count += 1
			else:
				a[i + 1] = 1
				count += 1

print(count)
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
n = int(input())
tree = [[] for i in range(n)]
for i in range(n):
    id, left, right = map(int, input().split())
    tree[id] = [left, right]

def preoder(id):
    if id == -1:
        return
    print(' ' + str(id), end='')
    preoder(tree[id][0])
    preoder(tree[id][1])

def inorder(id):
    if id == -1:
        return
    inorder(tree[id][0])
    print(' ' + str(id), end='')
    inorder(tree[id][1])

def postorder(id):
    if id == -1:
        return
    postorder(tree[id][0])
    postorder(tree[id][1])
    print(' ' + str(id), end='')

print('Preoder')
preoder(0)
print('')
print('Inorder')
inorder(0)
print('')
print('Postorder')
postorder(0)



Failing test case input:
9
0 1 4
1 2 5
2 -1 -1
3 -1 -1
4 3 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 0 1 2 5 6 7 4 3 8
Inorder
 2 1 6 5 7 0 3 4 8
Postorder
 2 6 7 5 1 3 8 4 0


Actual output:
Preoder
 0 1 2 5 6 7 4 3 8
Inorder
 2 1 6 5 7 0 3 4 8
Postorder
 2 6 7 5 1 3 8 4 0

Fixed submission (verdict: Accepted):
n = int(input())
tree = [[-1, -1, True] for i in range(n)]
for i in range(n):
    id, left, right = map(int, input().split())
    if left != -1:
        tree[id][0] = left
        tree[left][2] = False
    if right != -1:
        tree[id][1] = right
        tree[right][2] = False

def preoder(id):
    if id == -1:
        return
    print(' ' + str(id), end='')
    preoder(tree[id][0])
    preoder(tree[id][1])

def inorder(id):
    if id == -1:
        return
    inorder(tree[id][0])
    print(' ' + str(id), end='')
    inorder(tree[id][1])

def postorder(id):
    if id == -1:
        return
    postorder(tree[id][0])
    postorder(tree[id][1])
    print(' ' + str(id), end='')

root = 0
for i, node in enumerate(tree):
    if node[2] == True:
        root = i

print('Preorder')
preoder(root)
print('')
print('Inorder')
inorder(root)
print('')
print('Postorder')
postorder(root)
print('')
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
N = int(input())
tree = [None for _ in range(N)]
root = set(range(N))

for i in range(N):
    i, l, r = map(int,input().split())
    tree[i] = (l, r)
    root -= {l, r}

root_node = root.pop()

def preorder(i):
    if i == -1:
        return
    (l, r) = tree[i]
    print(" {}".format(i), end = "")
    preorder(l)
    preorder(r)
    
def inorder(i):
    if i == -1:
        return
    (l, r) = tree[i]
    inorder(l)
    print(" {}".format(i), end = "")
    inorder(r)
    
def postorder(i):
    if i == -1:
        return
    (l, r) = tree[i]
    inorder(l)
    inorder(r)
    print(" {}".format(i), end = "")
    
print('Preorder')
preorder(root_node)
print()

print('Inorder')
inorder(root_node)
print()

print('Postorder')
postorder(root_node)
print()

Failing test case input:
9
0 2 4
1 0 3
2 -1 -1
3 -1 0
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8 1 3 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0 2 6 7 5 8 4 0 3 1


Actual output:
Preorder
 1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8 1 3 2 0 6 5 7 4 8
Postorder
 2 0 6 5 7 4 8 3 2 0 6 5 7 4 8 1


Fixed submission (verdict: Accepted):
N = int(input())
tree = [None for _ in range(N)]
root = set(range(N))

for i in range(N):
    i, l, r = map(int,input().split())
    tree[i] = (l, r)
    root -= {l, r}

root_node = root.pop()

def preorder(i):
    if i == -1:
        return
    (l, r) = tree[i]
    print(" {}".format(i), end = "")
    preorder(l)
    preorder(r)
    
def inorder(i):
    if i == -1:
        return
    (l, r) = tree[i]
    inorder(l)
    print(" {}".format(i), end = "")
    inorder(r)
    
def postorder(i):
    if i == -1:
        return
    (l, r) = tree[i]
    postorder(l)
    postorder(r)
    print(" {}".format(i), end = "")
    
print('Preorder')
preorder(root_node)
print()

print('Inorder')
inorder(root_node)
print()

print('Postorder')
postorder(root_node)
print()
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
n = int(input())
tree = [[-1, -1, True] for i in range(n)]
for i in range(n):
    id, left, right = map(int, input().split())
    if left != -1:
        tree[id][0] = left
        tree[left][2] = False
    if right != -1:
        tree[id][1] = right
        tree[right][2] = False

def preoder(id):
    if id == -1:
        return
    print(' ' + str(id), end='')
    preoder(tree[id][0])
    preoder(tree[id][1])

def inorder(id):
    if id == -1:
        return
    inorder(tree[id][0])
    print(' ' + str(id), end='')
    inorder(tree[id][1])

def postorder(id):
    if id == -1:
        return
    postorder(tree[id][0])
    postorder(tree[id][1])
    print(' ' + str(id), end='')

root = 0
for i, node in enumerate(tree):
    if node[2] == True:
        root = i

print('Preoder')
preoder(root)
print('')
print('Inorder')
inorder(root)
print('')
print('Postorder')
postorder(root)



Failing test case input:
9
0 2 4
1 0 3
2 -1 -1
3 -1 0
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8 1 3 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0 2 6 7 5 8 4 0 3 1


Actual output:
Preoder
 1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8 1 3 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0 2 6 7 5 8 4 0 3 1

Fixed submission (verdict: Accepted):
n = int(input())
tree = [[-1, -1, True] for i in range(n)]
for i in range(n):
    id, left, right = map(int, input().split())
    if left != -1:
        tree[id][0] = left
        tree[left][2] = False
    if right != -1:
        tree[id][1] = right
        tree[right][2] = False

def preoder(id):
    if id == -1:
        return
    print(' ' + str(id), end='')
    preoder(tree[id][0])
    preoder(tree[id][1])

def inorder(id):
    if id == -1:
        return
    inorder(tree[id][0])
    print(' ' + str(id), end='')
    inorder(tree[id][1])

def postorder(id):
    if id == -1:
        return
    postorder(tree[id][0])
    postorder(tree[id][1])
    print(' ' + str(id), end='')

root = 0
for i, node in enumerate(tree):
    if node[2] == True:
        root = i

print('Preorder')
preoder(root)
print('')
print('Inorder')
inorder(root)
print('')
print('Postorder')
postorder(root)
print('')
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
n = int(input())
tree = [0] * n
root = set(range(n))
for i in range(n):
    node_id, left, right = map(int, input().split())
    tree[node_id] = (left, right)
    root -= {left, right}
root_node = root.pop()


def preorder(i):
    if i == -1:
        return
    (left, right) = tree[i]
    yield i
    yield from preorder(left)
    yield from preorder(right)


def inorder(i):
    if i == -1:
        return
    (left, right) = tree[i]
    yield from inorder(left)
    yield i
    yield from inorder(right)


def postorder(i):
    if i == -1:
        return
    (left, right) = tree[i]
    yield from postorder(left)
    yield from postorder(right)
    yield i

print("preorder\n ", end="")
print(*preorder(root_node))
print("inorder\n ", end="")
print(*inorder(root_node))
print("postorder\n ", end="")
print(*postorder(root_node))

Failing test case input:
9
0 2 4
1 1 3
2 -1 -1
3 -1 -1
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0


Actual output:
preorder
 0 2 4 5 6 7 8
inorder
 2 0 6 5 7 4 8
postorder
 2 6 7 5 8 4 0


Fixed submission (verdict: Accepted):
n = int(input())
tree = [0] * n
root = set(range(n))
for i in range(n):
    node_id, left, right = map(int, input().split())
    tree[node_id] = (left, right)
    root -= {left, right}
root_node = root.pop()


def preorder(i):
    if i == -1:
        return
    (left, right) = tree[i]
    yield i
    yield from preorder(left)
    yield from preorder(right)


def inorder(i):
    if i == -1:
        return
    (left, right) = tree[i]
    yield from inorder(left)
    yield i
    yield from inorder(right)


def postorder(i):
    if i == -1:
        return
    (left, right) = tree[i]
    yield from postorder(left)
    yield from postorder(right)
    yield i

print("Preorder\n ", end="")
print(*preorder(root_node))
print("Inorder\n ", end="")
print(*inorder(root_node))
print("Postorder\n ", end="")
print(*postorder(root_node))
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
import sys

def walk(T, nid, order):
  if nid < 0: return
  lid, rid = T[nid]

  if order == "pre":
    sys.stdout.write(" " + str(nid))
    walk(T, lid, order)
    walk(T, rid, order)
  elif order == "in":
    walk(T, lid, order)
    sys.stdout.write(" " + str(nid))
    walk(T, rid, order)
  else:
    walk(T, lid, order)
    walk(T, rid, order)
    sys.stdout.write(" " + str(nid))

T = {}
for i in range(int(input())):
  nid, lid, rid = map(int, input().split())
  T[nid] = [lid, rid]

print("Preorder"),  walk(T, 0, "pre"),  print()
print("Inorder"),   walk(T, 0, "in"),   print()
print("Postorder"), walk(T, 0, "post"), print()

Failing test case input:
9
0 2 4
1 0 3
2 -1 -1
3 -1 -1
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 1 0 2 4 5 6 7 8 3
Inorder
 2 0 6 5 7 4 8 1 3
Postorder
 2 6 7 5 8 4 0 3 1


Actual output:
Preorder
 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0


Fixed submission (verdict: Accepted):
import sys

def walk(T, nid, order):
  if nid < 0: return
  pid, lid, rid = T[nid]

  if order == "pre":
    sys.stdout.write(" " + str(nid))
    walk(T, lid, order)
    walk(T, rid, order)
  elif order == "in":
    walk(T, lid, order)
    sys.stdout.write(" " + str(nid))
    walk(T, rid, order)
  else:
    walk(T, lid, order)
    walk(T, rid, order)
    sys.stdout.write(" " + str(nid))

T = {}
for i in range(int(input())):
  nid, lid, rid = map(int, input().split())
  T[nid] = [-1, lid, rid]


for nid in T:
  pid, lid, rid = T[nid]
  if lid >= 0: T[lid][0] = nid
  if rid >= 0: T[rid][0] = nid


for nid in T:
  if T[nid][0] == -1: root_id = nid

print("Preorder"),  walk(T, root_id, "pre"),  print()
print("Inorder"),   walk(T, root_id, "in"),   print()
print("Postorder"), walk(T, root_id, "post"), print()
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
from collections import defaultdict
def preorder(here, conn, chain):
    if here == -1:
        return
    chain.append(here)
    if conn[here]:
        preorder(conn[here][0], conn, chain)
        preorder(conn[here][1], conn, chain)

def inorder(here, conn, chain):
    if here == -1:
        return
    if conn[here]:
        inorder(conn[here][0], conn, chain)
        chain.append(here)
        inorder(conn[here][1], conn, chain)

def postorder(here, conn, chain):
    if here == -1:
        return
    if conn[here]:
        postorder(conn[here][0], conn, chain)
        postorder(conn[here][1], conn, chain)
    chain.append(here)

query = int(input())
connect = defaultdict(list)
for _ in range(query):
    here, left, right = (int(n) for n in input().split(" "))
    connect[here] = [left, right]
preo = []
ino = []
posto = []
preorder(0, connect, preo)
inorder(0, connect, ino)
postorder(0, connect, posto)

print("Preorder")
print(*preo)
print("Inorder")
print(*ino)
print("Postorder")
print(*posto)

Failing test case input:
9
0 2 4
1 0 3
2 -1 -1
3 -1 0
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8 1 3 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0 2 6 7 5 8 4 0 3 1


Actual output:
Preorder
0 2 4 5 6 7 8
Inorder
2 0 6 5 7 4 8
Postorder
2 6 7 5 8 4 0


Fixed submission (verdict: Accepted):
from collections import defaultdict
def preorder(here, conn, chain):
    if here == -1:
        return
    chain.append(here)
    if conn[here]:
        preorder(conn[here][0], conn, chain)
        preorder(conn[here][1], conn, chain)

def inorder(here, conn, chain):
    if here == -1:
        return
    if conn[here]:
        inorder(conn[here][0], conn, chain)
        chain.append(here)
        inorder(conn[here][1], conn, chain)

def postorder(here, conn, chain):
    if here == -1:
        return
    if conn[here]:
        postorder(conn[here][0], conn, chain)
        postorder(conn[here][1], conn, chain)
    chain.append(here)

query = int(input())
connect = defaultdict(list)
in_v = [0 for n in range(query + 1)]
for _ in range(query):
    here, left, right = (int(n) for n in input().split(" "))
    connect[here] = [left, right]
    in_v[left] += 1
    in_v[right] += 1
for i in range(query):
    if not in_v[i]:
        root = i
        break
preo = []
ino = []
posto = []
preorder(root, connect, preo)
inorder(root, connect, ino)
postorder(root, connect, posto)

print("Preorder")
print("", end = " ")
print(*preo)
print("Inorder")
print("", end = " ")
print(*ino)
print("Postorder")
print("", end = " ")
print(*posto)
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
from sys import stdin

class Node(object):
    def __init__(self, parent=None, left=None, right=None):
        self.parent = parent
        self.left = left
        self.right = right

def print_nodes(nodes, n):
    A = []
    B = []
    C = []
    def walk_tree(nodes, u):
        if u == -1:
            return
        r = nodes[u].right
        l = nodes[u].left
        nonlocal A
        A.append(u)
        walk_tree(nodes, l)
        B.append(u)
        walk_tree(nodes, r)
        C.append(u)

    for i in range(n):
        if nodes[i].parent == None:
            walk_tree(nodes, i)
            print("Preorder", end="\n ")
            print(*A, sep=" ")
            print("Ineorder", end="\n ")
            print(*B, sep=" ")
            print("Postorder", end="\n ")
            print(*C, sep=" ")

def read_binary_tree(nodes, n):
    for _ in range(n):
        i = [int(i) for i in stdin.readline().strip().split()]
        nodes[i[0]].left = i[1]
        nodes[i[0]].right = i[2]
        if i[1] != -1:
            nodes[i[1]].parent = i[0]
        if i[2] != -1:
            nodes[i[2]].parent = i[0]

n = int(input())
nodes = [Node() for _ in range(n)]
read_binary_tree(nodes, n)
print_nodes(nodes, n)


Failing test case input:
9
0 2 4
1 0 3
2 -1 -1
3 -1 -1
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 1 0 2 4 5 6 7 8 3
Inorder
 2 0 6 5 7 4 8 1 3
Postorder
 2 6 7 5 8 4 0 3 1


Actual output:
Preorder
 1 0 2 4 5 6 7 8 3
Ineorder
 2 0 6 5 7 4 8 1 3
Postorder
 2 6 7 5 8 4 0 3 1


Fixed submission (verdict: Accepted):
from sys import stdin

class Node(object):
    def __init__(self, parent=None, left=None, right=None):
        self.parent = parent
        self.left = left
        self.right = right

def print_nodes(nodes, n):
    A = []
    B = []
    C = []
    def walk_tree(nodes, u):
        if u == -1:
            return
        r = nodes[u].right
        l = nodes[u].left
        nonlocal A
        A.append(u)
        walk_tree(nodes, l)
        B.append(u)
        walk_tree(nodes, r)
        C.append(u)

    for i in range(n):
        if nodes[i].parent == None:
            walk_tree(nodes, i)
            print("Preorder", end="\n ")
            print(*A, sep=" ")
            print("Inorder", end="\n ")
            print(*B, sep=" ")
            print("Postorder", end="\n ")
            print(*C, sep=" ")

def read_binary_tree(nodes, n):
    for _ in range(n):
        i = [int(i) for i in stdin.readline().strip().split()]
        nodes[i[0]].left = i[1]
        nodes[i[0]].right = i[2]
        if i[1] != -1:
            nodes[i[1]].parent = i[0]
        if i[2] != -1:
            nodes[i[2]].parent = i[0]

n = int(input())
nodes = [Node() for _ in range(n)]
read_binary_tree(nodes, n)
print_nodes(nodes, n)
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
n = int(input())
tree = [[-1, -1, True] for i in range(n)]
for i in range(n):
    id, left, right = map(int, input().split())
    if left != -1:
        tree[id][0] = left
        tree[left][2] = False
    if right != -1:
        tree[id][1] = right
        tree[right][2] = False

def preoder(id):
    if id == -1:
        return
    print(' ' + str(id), end='')
    preoder(tree[id][0])
    preoder(tree[id][1])

def inorder(id):
    if id == -1:
        return
    inorder(tree[id][0])
    print(' ' + str(id), end='')
    inorder(tree[id][1])

def postorder(id):
    if id == -1:
        return
    postorder(tree[id][0])
    postorder(tree[id][1])
    print(' ' + str(id), end='')

for i, node in enumerate(tree):
    if node[2] == True:
        root = i

print('Preoder')
preoder(root)
print('')
print('Inorder')
inorder(root)
print('')
print('Postorder')
postorder(root)



Failing test case input:
9
0 2 4
1 0 3
2 -1 -1
3 0 0
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8 1 2 0 6 5 7 4 8 3 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0 2 6 7 5 8 4 0 2 6 7 5 8 4 0 3 1


Actual output:
Preoder
 1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8 1 2 0 6 5 7 4 8 3 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0 2 6 7 5 8 4 0 2 6 7 5 8 4 0 3 1

Fixed submission (verdict: Accepted):
n = int(input())
tree = [[-1, -1, True] for i in range(n)]
for i in range(n):
    id, left, right = map(int, input().split())
    if left != -1:
        tree[id][0] = left
        tree[left][2] = False
    if right != -1:
        tree[id][1] = right
        tree[right][2] = False

def preoder(id):
    if id == -1:
        return
    print(' ' + str(id), end='')
    preoder(tree[id][0])
    preoder(tree[id][1])

def inorder(id):
    if id == -1:
        return
    inorder(tree[id][0])
    print(' ' + str(id), end='')
    inorder(tree[id][1])

def postorder(id):
    if id == -1:
        return
    postorder(tree[id][0])
    postorder(tree[id][1])
    print(' ' + str(id), end='')

root = 0
for i, node in enumerate(tree):
    if node[2] == True:
        root = i

print('Preorder')
preoder(root)
print('')
print('Inorder')
inorder(root)
print('')
print('Postorder')
postorder(root)
print('')
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
from collections import namedtuple

class TreeWalk:
    def __init__(self, t):
        self.t = t

    def preParse(self, u):
        if u == -1:
            return 
        print(" ", str(u), end = "")
        self.preParse(self.t[u].l)
        self.preParse(self.t[u].r)

    def inParse(self, u):
        if u == -1:
            return 
        self.inParse(self.t[u].l)
        print(" ", str(u), end = "")
        self.inParse(self.t[u].r)

    def postParse(self, u):
        if u == -1:
            return 
        self.inParse(self.t[u].l)
        self.inParse(self.t[u].r)
        print(" ", str(u), end = "")
    
if __name__ == '__main__':
    n = int(input().rstrip())
    Node = namedtuple('Node', ['p', 'l', 'r'])
    t = [Node(-1, -1, -1)] * n
    root = -1
    for i in range(n):
        v, l, r = [int(i) for i in input().rstrip().split(" ")]
        t[v] = t[v]._replace(l = l, r = r)
        if l != -1:
            t[l] = t[l]._replace(p = v)
        if r != -1:
            t[r] = t[r]._replace(p = v)
    for i in range(n):
        if (t[i].p == -1):
            root = i

    x = TreeWalk(t)
    print("Preorder")
    x.preParse(root)
    print()
    print("Inorder")
    x.inParse(root)
    print()
    print("Postorder")
    x.postParse(root)

Failing test case input:
9
0 2 4
1 0 3
2 -1 -1
3 0 0
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8 1 2 0 6 5 7 4 8 3 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0 2 6 7 5 8 4 0 2 6 7 5 8 4 0 3 1


Actual output:
Preorder
  1  0  2  4  5  6  7  8  3  0  2  4  5  6  7  8  0  2  4  5  6  7  8
Inorder
  2  0  6  5  7  4  8  1  2  0  6  5  7  4  8  3  2  0  6  5  7  4  8
Postorder
  2  0  6  5  7  4  8  2  0  6  5  7  4  8  3  2  0  6  5  7  4  8  1

Fixed submission (verdict: Accepted):
from collections import namedtuple

class TreeWalk:
    def __init__(self, t):
        self.t = t

    def preParse(self, u):
        if u == -1:
            return 
        print("", str(u), end = "")
        self.preParse(self.t[u].l)
        self.preParse(self.t[u].r)

    def inParse(self, u):
        if u == -1:
            return 
        self.inParse(self.t[u].l)
        print("", str(u), end = "")
        self.inParse(self.t[u].r)

    def postParse(self, u):
        if u == -1:
            return 
        self.postParse(self.t[u].l)
        self.postParse(self.t[u].r)
        print("", str(u), end = "")
    
if __name__ == '__main__':
    n = int(input().rstrip())
    Node = namedtuple('Node', ['p', 'l', 'r'])
    t = [Node(-1, -1, -1)] * n
    root = -1
    for i in range(n):
        v, l, r = [int(i) for i in input().rstrip().split(" ")]
        t[v] = t[v]._replace(l = l, r = r)
        if l != -1:
            t[l] = t[l]._replace(p = v)
        if r != -1:
            t[r] = t[r]._replace(p = v)
    for i in range(n):
        if (t[i].p == -1):
            root = i

    x = TreeWalk(t)
    print("Preorder")
    x.preParse(root)
    print()
    print("Inorder")
    x.inParse(root)
    print()
    print("Postorder")
    x.postParse(root)
    print()
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
def s(x):
    print('',x,end='')
def p(u):
    if u+1:s(u);p(R[u]);p(L[u])
def i(u):
    if u+1:i(R[u]);s(u);i(L[u])
def o(u):
    if u+1:o(R[u]);o(L[u]);s(u)
        
n = int(input())
R, L = [0]*n, [0]*n
for _ in range(n):
    a, b, c = map(int, input().split())
    R[a] = b
    L[a] = c
print(R)
print(L)
root = (set(range(n))-set(R)-set(L)).pop()
print('Preorder');p(root)
print('\nInorder');i(root)
print('\nPostorder');o(root)
print()
     


Failing test case input:
9
0 2 4
1 0 3
2 -1 -1
3 0 0
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8 1 2 0 6 5 7 4 8 3 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0 2 6 7 5 8 4 0 2 6 7 5 8 4 0 3 1


Actual output:
[2, 0, -1, 0, 5, 6, -1, -1, -1]
[4, 3, -1, 0, 8, 7, -1, -1, -1]
Preorder
 1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8 1 2 0 6 5 7 4 8 3 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0 2 6 7 5 8 4 0 2 6 7 5 8 4 0 3 1


Fixed submission (verdict: Accepted):
def s(x):
    print('',x,end='')
def p(u):
    if u+1:s(u);p(R[u]);p(L[u])
def i(u):
    if u+1:i(R[u]);s(u);i(L[u])
def o(u):
    if u+1:o(R[u]);o(L[u]);s(u)
        
n = int(input())
R, L = [0]*n, [0]*n
for _ in range(n):
    a, b, c = map(int, input().split())
    R[a] = b
    L[a] = c

root = (set(range(n))-set(R)-set(L)).pop()
print('Preorder');p(root)
print('\nInorder');i(root)
print('\nPostorder');o(root)
print()
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
n = int(input())
root = set(range(n))
nodes = [0 for i in range(n)]
for i in range(n):
    node = list(map(int, input().split()))
    children = node[1:3]
    root -= set(children)
    nodes[node[0]] = children

def preorder(id):
    if(id == -1):
        return
    order.append(id)
    preorder(nodes[id][0])
    preorder(nodes[id][1])

def inorder(id):
    if(id == -1):
        return
    inorder(nodes[id][0])
    order.append(id)
    inorder(nodes[id][1])

def postorder(id):
    if(id == -1):
        return
    postorder(nodes[id][0])
    postorder(nodes[id][1])
    order.append(id)

order = []
preorder(list(root)[0])
print(*order)
order = []
inorder(list(root)[0])
print(*order)
order = []
postorder(list(root)[0])
print(*order)



Failing test case input:
9
0 2 4
1 0 3
2 -1 -1
3 0 0
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8 1 2 0 6 5 7 4 8 3 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0 2 6 7 5 8 4 0 2 6 7 5 8 4 0 3 1


Actual output:
1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8 0 2 4 5 6 7 8
2 0 6 5 7 4 8 1 2 0 6 5 7 4 8 3 2 0 6 5 7 4 8
2 6 7 5 8 4 0 2 6 7 5 8 4 0 2 6 7 5 8 4 0 3 1


Fixed submission (verdict: Accepted):
n = int(input())
root = set(range(n))
nodes = [0 for i in range(n)]
for i in range(n):
    node = list(map(int, input().split()))
    children = node[1:3]
    root -= set(children)
    nodes[node[0]] = children

def preorder(id):
    if(id == -1):
        return
    order.append(id)
    preorder(nodes[id][0])
    preorder(nodes[id][1])

def inorder(id):
    if(id == -1):
        return
    inorder(nodes[id][0])
    order.append(id)
    inorder(nodes[id][1])

def postorder(id):
    if(id == -1):
        return
    postorder(nodes[id][0])
    postorder(nodes[id][1])
    order.append(id)

order = []
preorder(list(root)[0])
print('Preorder')
print(' ', end='')
print(*order)
order = []
inorder(list(root)[0])
print('Inorder')
print(' ', end='')
print(*order)
order = []
postorder(list(root)[0])
print('Postorder')
print(' ', end='')
print(*order)
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
class Node:
    def __init__(self, id):
        self.id = id
        self.left = None
        self.right = None

root = None
n = int(input())
node_list = []
for i in range(n):
    node_list.append(Node(i))

for i in range(n):
    id, left, right = list(map(int, input().split()))
    node = node_list[id]
    if left > -1:
        node.left = node_list[left]
    if right > -1:
        node.right = node_list[right]
    if id == 0:
        root = node

def preorder(node):
    if node is None:
        return
    print(' ' + str(node.id), end='')
    preorder(node.left)
    preorder(node.right)

def inorder(node):
    if node is None:
        return
    
    inorder(node.left)
    print(' ' + str(node.id), end='')
    inorder(node.right)

def postorder(node):
    if node is None:
        return
    print(' ' + str(node.id), end='')
    postorder(node.left)
    postorder(node.right)

print('Preorder')
preorder(root)
print('')

print('Inorder')
inorder(root)
print('')

print('Postorder')
postorder(root)
print('')


Failing test case input:
9
0 2 4
1 1 3
2 -1 -1
3 -1 -1
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0


Actual output:
Preorder
 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8
Postorder
 0 2 4 5 6 7 8


Fixed submission (verdict: Accepted):
class Node:
    def __init__(self, id):
        self.id = id
        self.parent = None
        self.left = None
        self.right = None

root = None
n = int(input())
node_list = []
for i in range(n):
    node_list.append(Node(i))

for i in range(n):
    id, left, right = list(map(int, input().split()))
    node = node_list[id]
    if left > -1:
        node_list[left].parent = node
        node.left = node_list[left]
    if right > -1:
        node_list[right].parent = node
        node.right = node_list[right]

root = None
for node in node_list:
    if node.parent is None:
        root = node
        break

def preorder(node):
    if node is None:
        return
    print(' ' + str(node.id), end='')
    preorder(node.left)
    preorder(node.right)

def inorder(node):
    if node is None:
        return
    inorder(node.left)
    print(' ' + str(node.id), end='')
    inorder(node.right)

def postorder(node):
    if node is None:
        return
    postorder(node.left)
    postorder(node.right)
    print(' ' + str(node.id), end='')

print('Preorder')
preorder(root)
print('')

print('Inorder')
inorder(root)
print('')

print('Postorder')
postorder(root)
print('')
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):

class Tree:
    def __init__(self,parent,left,right):
        self.parent = parent
        self.left = left
        self.right = right


def preparse(u):
    if u == -1: return
    print(" ", end="")
    print(u, end="")
    preparse(trees[u].left)
    preparse(trees[u].right)


def inparse(u):
    if u == -1: return
    
    inparse(trees[u].left)
    
    print(" ", end="")
    print(u, end="")
    
    inparse(trees[u].right)
    

def postparse(u):
    if u == -1: return
    
    postparse(trees[u].left)
    
    postparse(trees[u].right)
    
    print(" ", end="")
    print(u, end="")


N = int(input())

trees = [Tree(-1,-1,-1) for i in range(N)]
for i in range(N):
    l = list(map(int, input().split()))
    no = l[0]
    left = l[1]
    right = l[2]
    trees[no].left = left
    trees[no].right = right
    if left != -1: trees[left].parent = no
    if right != -1: trees[right].parent = no

print("Preorder")
preparse(0)
print("")
print("Inorder")
inparse(0)
print("")
print("Postorder")
postparse(0)

Failing test case input:
9
0 2 4
1 0 3
2 -1 -1
3 -1 0
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 1 0 2 4 5 6 7 8 3 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8 1 3 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0 2 6 7 5 8 4 0 3 1


Actual output:
Preorder
 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0

Fixed submission (verdict: Accepted):

class Tree:
    def __init__(self,parent,left,right):
        self.parent = parent
        self.left = left
        self.right = right


def preparse(u):
    if u == -1: return
    print(" ", end="")
    print(u, end="")
    preparse(trees[u].left)
    preparse(trees[u].right)


def inparse(u):
    if u == -1: return
    
    inparse(trees[u].left)
    
    print(" ", end="")
    print(u, end="")
    
    inparse(trees[u].right)
    

def postparse(u):
    if u == -1: return
    
    postparse(trees[u].left)
    
    postparse(trees[u].right)
    
    print(" ", end="")
    print(u, end="")


N = int(input())

trees = [Tree(-1,-1,-1) for i in range(N)]
for i in range(N):
    l = list(map(int, input().split()))
    no = l[0]
    left = l[1]
    right = l[2]
    trees[no].left = left
    trees[no].right = right
    if left != -1: trees[left].parent = no
    if right != -1: trees[right].parent = no

r = 0
for i in range(N):
    if trees[i].parent == -1:
        r = i

print("Preorder")
preparse(r)
print("")
print("Inorder")
inparse(r)
print("")
print("Postorder")
postparse(r)
print("")