Data Structures & AlgorithmsModerateAmazon · Microsoft OA
A binary tree with 12 nodes is serialised in preorder, writing '#' for every missing child. How many '#' markers appear?
- A. 12
- B. 13
- C. 11
- D. 24
Correct answer: B. 13
Each of the n nodes has 2 child slots, 2n in total, and exactly n − 1 of them are filled by real nodes (every node except the root has a parent). So 2n − (n − 1) = n + 1 = 13 slots are empty. This is also why a valid serialisation has one more '#' than nodes.
More Coding Interview Problems questions
- Problem: Return the diameter of a binary tree (number of EDGES on the longest path between any two nodes). python class T: def init(s, v)…
- Problem: A node of a directed graph is 'safe' if every path from it ends at a terminal node (no outgoing edges). List the safe nodes…
- Problem: Find the bridges and articulation points of an undirected graph using DFS discovery times and low-links. python import sys…
- Problem: Count the strongly connected components and the number of edges in the condensation DAG (Kosaraju). python n = 8 E =…
- Problem: Check whether each graph is bipartite with BFS 2-colouring (graphs may be disconnected). python from collections import deque def…
- Problem: Burst balloons; bursting balloon i earns left × a[i] × right using its current neighbours (1 beyond the ends). Maximise the…