Data Structures & AlgorithmsHardGATE CS · Product-company placement
A binary search tree holds exactly the keys 16, 17, 41, 44, 69, 81, 82, 95. Which ONE of the following sequences can be its PREORDER traversal?
- A. 81, 69, 41, 17, 16, 44, 95, 82
- B. 81, 69, 41, 82, 16, 44, 95, 17
- C. 81, 82, 41, 17, 16, 44, 95, 69
- D. 81, 69, 95, 17, 16, 44, 41, 82
Correct answer: A. 81, 69, 41, 17, 16, 44, 95, 82
In a valid BST preorder, once a key larger than some node appears (we have moved into that node's right subtree), no later key may be smaller than that node. 81, 69, 41, 17, 16, 44, 95, 82 never breaks this; each other option does at some point.
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
- In the DAG with edges A→D, A→F, C→B, D→B, D→C, D→E, D→F, D→G, E→B, G→E, G→F, how many distinct directed paths are there from A to F, and…
- An undirected graph has edges A–B, A–G, B–E, C–E, C–F, C–G, D–E, D–F, D–G. An ITERATIVE DFS starts with stack [A]; it pops a vertex, skips…
- What is the length of the shortest string that has BOTH COMPUTER and SUBSEQUENT as subsequences (shortest common supersequence)?
- A rooted tree has 6 nodes with 1 child, 6 nodes with 2 children, 3 nodes with 3 children, and every other node is a leaf. How many leaves…
- Edit distance between HEAPSORT and DYNAMIC where insert and delete cost 1 each but a substitution costs 2: what is the minimum total cost?
- How many topological orderings does the DAG on vertices A–F with edges C→A, D→A, E→A, E→D, F→C, F→D, F→E have?