Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern

A binary tree is stored in level order in a 0-indexed array (children of i at 2i + 1 and 2i + 2; None = no node): [1, 2, 3, None, 4, 5]. What is its inorder traversal?

  1. A. 4, 2, 1, 5, 3
  2. B. 2, 4, 1, 5, 3
  3. C. 2, 4, 1, 3, 5
  4. D. 1, 2, 4, 3, 5

Correct answer: B. 2, 4, 1, 5, 3

Index 1 (value 2) has no left child (index 3 is None) and right child 4 (index 4). Index 2 (value 3) has left child 5 (index 5). Inorder: 2, 4, 1, 5, 3. Placing 4 as 2's LEFT child (ignoring the None gap) gives 4, 2, 1, …

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic