ProgrammingVery hardGATE CS · Product company OA

With the tree below, what does in(0) print?

With the tree below, what does in(0) print?
char key[] = "ABCDE";
int L[] = {1, 3, -1, -1, -1}, R[] = {2, 4, -1, -1, -1};
void in(int n) {
    if (n < 0) return;
    in(L[n]);
    putchar(key[n]);
    in(R[n]);
}
  1. A. DBAEC
  2. B. DBEAC
  3. C. ABDEC
  4. D. DEBCA

Correct answer: B. DBEAC

A is the root with left child B and right child C; B has children D and E. In-order visits left subtree, node, right subtree: D B E, then A, then C. ABDEC is pre-order and DEBCA post-order.

More C Programming questions

Exams that ask this topic