Data Structures & AlgorithmsEasyTCS NQT · Infosys pattern

A binary tree has preorder A B D E C F G and inorder D B E A F C G. What is its postorder?

A binary tree has preorder A B D E C F G and inorder D B E A F C G. What is its postorder?
def post(pre, ino):
    if not pre: return ""
    r = pre[0]; i = ino.index(r)
    return post(pre[1:i + 1], ino[:i]) + post(pre[i + 1:], ino[i + 1:]) + r
print(post("ABDECFG", "DBEAFCG"))
  1. A. DBEFCGA
  2. B. DEBGFCA
  3. C. EDBFGCA
  4. D. DEBFGCA

Correct answer: D. DEBFGCA

A is the root; inorder splits D B E (left) and F C G (right). B roots the left subtree with children D and E, C roots the right with F and G. Postorder: D E B F G C A.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic