Data Structures & AlgorithmsHardCodeforces Div2 B · ICPC regional
A rooted tree has these parent→child edges: 71→74, 77→70, 71→40, 74→13, 40→2, 74→5, 71→1, 77→73, 40→20, 1→77. A depth-first search starts at the root and, at every node, visits its children in increasing order of label. Give the preorder (a node is printed before every node of its subtree) as a sequence of node labels.
- A. 71 74 13 5 40 20 2 1 77 73 70
- B. 70 73 77 1 2 20 40 5 13 74 71
- C. 71 1 77 70 73 40 2 20 74 5 13
- D. 71 1 40 74 77 2 20 5 13 70 73
Correct answer: C. 71 1 77 70 73 40 2 20 74 5 13
Preorder is decided by where a node is printed relative to its subtree; with children in increasing label order the sequence is 71 1 77 70 73 40 2 20 74 5 13. Reversing the child order or swapping the two traversals are the usual slips.
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
- A rooted tree has these parent→child edges: 33→61, 68→22, 40→11, 61→87, 34→68, 61→35, 22→33, 34→83, 34→40. A depth-first search starts at…
- A rooted tree has these parent→child edges: 50→23, 70→50, 50→26, 50→19, 70→85, 75→64, 85→75. A depth-first search starts at the root and…
- A rooted tree has these parent→child edges: 82→55, 82→42, 59→37, 42→94, 55→1, 82→59, 42→87. A depth-first search starts at the root and…
- A rooted tree has these parent→child edges: 95→93, 88→72, 93→20, 37→88, 37→95, 20→31, 66→37. A depth-first search starts at the root and…
- A rooted tree has these parent→child edges: 20→69, 75→47, 60→40, 60→20, 47→96, 74→32, 74→75, 74→60, 20→46, 47→36. A depth-first search…
- A rooted tree has these parent→child edges: 15→97, 97→40, 48→32, 15→43, 15→70, 48→15, 36→90, 36→16, 32→36, 70→96. A depth-first search…