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"))- A. DBEFCGA
- B. DEBGFCA
- C. EDBFGCA
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- How many structurally different binary trees have n = 4 nodes, and how many distinct labelled binary trees can be formed from 4 distinct…
- What is printed? (number of nodes in a complete binary tree in O(log² n)) python def count(nnodes): # implicit complete tree with nodes…
- What is printed? (AVL: which rotation is needed after inserting 30, 10, 20 into an empty AVL tree?) python def balancecase(order): a, b, c…
- What is the minimum number of nodes in an AVL tree of height h (height of a single node = 0)? python def nmin(h): a, b = 1, 2 # N(0), N(1)…
- BFS from vertex 0 visits neighbours in increasing order. What is the BFS order and the DFS (recursive) order? python g = {0: [1, 2], 1…
- Run Dijkstra from A on the weighted directed graph. In what order are vertices finalised (popped), and what is dist[E]? python import…