Reconstruction of a Tree

Write a program that takes two sequences of nodes obtained by the preorder and inorder tree walks on a binary tree, and outputs a sequence of nodes obtained by the postorder tree walk on the binary tree.

Input:
- An integer n, the number of nodes in the binary tree.
- The sequence of node IDs obtained by the preorder tree walk, separated by spaces.
- The sequence of node IDs obtained by the inorder tree walk, separated by spaces.

Output:
- Print the sequence of node IDs obtained by the postorder tree walk in a single line, with a single space character between adjacent IDs.

Constraints:
- 1 ≤ n ≤ 40

Sample Input 1:
5
1 2 3 4 5
3 2 4 1 5

Sample Output 1:
3 4 2 5 1

Sample Input 2:
4
1 2 3 4
1 2 3 4

Sample Output 2:
4 3 2 1

# Buggy version:
n = int(input())
pre = list(map(int, input().split()))
ino = list(map(int, input().split()))

d = []
root = pre[1]
pre = iter(pre).__next__

def dfs(l, r):
    if l >= r:
        return
    c = pre()
    m = ino.index(c)
    dfs(l, m)
    dfs(m+1, r)
    d += [c]
    
dfs(0, len(ino))
print(*d)



# Fixed version:
n = int(input())
pre = list(map(int, input().split()))
ino = list(map(int, input().split()))

d = []
root = pre[0]
pre = iter(pre).__next__

def dfs(l, r):
    global d
    if l >= r:
        return
    c = pre()
    m = ino.index(c)
    dfs(l, m)
    dfs(m+1, r)
    d += [c]
    
dfs(0, len(ino))
print(*d)

# Buggy version:
#coding:UTF-8

def RoT(A,B,ans):
    if A:
        root=A[0]
        rindex=B.index(root)
        Aleft=A[1:rindex+1]
        Aright=A[rindex+1:]
        Bleft=B[:rindex]
        Bright=B[rindex:]
        RoT(Aleft,Bleft,ans)
        RoT(Aright,Bright,ans)
        ans.append(root)
        
        


if __name__=="__main__":
    n=int(input())
    A=input().split(" ")
    B=input().split(" ")
    ans=[]
    RoT(A,B,ans)
    print(" ".join(ans))

# Fixed version:
#coding:UTF-8

def RoT(A,B,ans):
    if A:
        root=A[0]
        rindex=B.index(root)
        Aleft=A[1:rindex+1]
        Aright=A[rindex+1:]
        Bleft=B[:rindex]
        Bright=B[rindex+1:]
        RoT(Aleft,Bleft,ans)
        RoT(Aright,Bright,ans)
        ans.append(root)
        
        


if __name__=="__main__":
    n=int(input())
    A=input().split(" ")
    B=input().split(" ")
    ans=[]
    RoT(A,B,ans)
    print(" ".join(ans))

# Buggy version:
def getPostorder(preorder, inorder):
    if not preorder:
        return []
    postorder = []
    root = preorder[0]
    rootIndex = inorder.index(root)

    postorder.extend(getPostorder(preorder[1:rootIndex + 1], inorder[:rootIndex]))
    postorder.extend(getPostorder(preorder[rootIndex + 1:], inorder[rootIndex + 1:]))
    postorder.append(root)

    return postorder 

if __name__ == '__main__':
    n = int(input())
    preorder = list(map(int, input().split()))
    inorder = list(map(int, input().split()))
    print(getPostorder(preorder, inorder))

# Fixed version:
def getPostorder(preorder, inorder):
    if not preorder:
        return []
    postorder = []
    root = preorder[0]
    rootIndex = inorder.index(root)

    postorder.extend(getPostorder(preorder[1:rootIndex + 1], inorder[:rootIndex]))
    postorder.extend(getPostorder(preorder[rootIndex + 1:], inorder[rootIndex + 1:]))
    postorder.append(root)

    return postorder 

if __name__ == '__main__':
    n = int(input())
    preorder = list(map(int, input().split()))
    inorder = list(map(int, input().split()))
    print(*getPostorder(preorder, inorder))

# Buggy version:
n = int(input())
tree = [None for i in range(n +1)]
list_pre = list(map(int, input().split()))
list_in = list(map(int, input().split()))
 
 
def reconstruct(tree_pre, tree_in):
    if not tree_pre:
        return -1
    root = tree_pre[0]
    idx = tree_in.index(root)
    tree[root] = (reconstruct(tree_pre[1:i + 1], tree_in[:i]), 
                  reconstruct(tree_pre[i + 1:],  tree_in[i + 1:]))
    return root


def post_order_search(i):
    if i == -1:
        return
    l, r = tree[i]
    for j in post_order_search(l):
        yield j
    for j in post_order_search(r):
        yield j
    yield i


solve(list_pre, list_in)

print(*postorder(list_pre[0]))

# Fixed version:
n = int(input())
tree = [None for i in range(n +1)]
list_pre = list(map(int, input().split()))
list_in = list(map(int, input().split()))
 
 
def solve(tree_pre, tree_in):
    if not tree_pre:
        return -1
    root = tree_pre[0]
    idx = tree_in.index(root)
    tree[root] = (solve(tree_pre[1:idx + 1], tree_in[:idx]), 
                  solve(tree_pre[idx + 1:],  tree_in[idx + 1:]))
    return root


def post_order_search(i):
    if i == -1:
        return
    l, r = tree[i]
    for j in post_order_search(l):
        yield j
    for j in post_order_search(r):
        yield j
    yield i


solve(list_pre, list_in)

print(*post_order_search(list_pre[0]))

# Buggy version:
def reconstruct(pre, in_):
    if not pre:
        return -1
    root = pre[0]
    i = in_.index(root)
    tree[root] = (reconstruct(pre[1 : i+1], in_[:i]), reconstruct(pre[i+1:], in_[i+1:]))
    return root

def postorder(i):
    if i == -1:
        return
    l, r = tree[i]
    for v in postorder(l):
        yield v
    for v in postorder(r):
        yield v
    yield i

n = int(input())
tree = [None for i in range(n+1)]
pre_tree = list(map(int, input().split()))
in_tree = list(map(int, input().split()))
reconstruct(pre_tree, in_tree)
print(tree)
print(*postorder(pre_tree[0]))


# Fixed version:
def reconstruct(pre, in_):
    if not pre:
        return -1
    root = pre[0]
    i = in_.index(root)
    tree[root] = (reconstruct(pre[1 : i+1], in_[:i]), reconstruct(pre[i+1:], in_[i+1:]))
    return root

def postorder(i):
    if i == -1:
        return
    l, r = tree[i]
    for v in postorder(l):
        yield v
    for v in postorder(r):
        yield v
    yield i

n = int(input())
tree = [None for i in range(n+1)]
pre_tree = list(map(int, input().split()))
in_tree = list(map(int, input().split()))
reconstruct(pre_tree, in_tree)
print(*postorder(pre_tree[0]))