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?
- A. 4, 2, 1, 5, 3
- B. 2, 4, 1, 5, 3
- C. 2, 4, 1, 3, 5
- 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, …
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
- The minimum is deleted from the min-heap [2, 5, 3, 9, 6, 4, 8] (0-indexed): the last element moves to the root and sifts down, swapping…
- An undirected graph has 6 vertices with degrees 3, 3, 2, 2, 2 and x, and it has 8 edges. What is x?
- Dijkstra's algorithm runs from S on the directed graph S→A (7), S→B (2), B→A (3), B→C (8), A→C (1), A→D (6), C→D (2). In what order are…
- What is the maximum number of edges in a directed ACYCLIC graph with 6 vertices (no parallel edges)?
- What is the length of the longest common subsequence of PLACEMENT and COMPLETE?
- Keys 15, 10, 20, 8, 12, 25, 6, 11, 13, 9 are inserted into an empty BST. What is the height of the tree (edges on the longest root-to-leaf…