The number of ways in which the numbers 1, 2, 3, 4, 5, 6, 7 can be inserted in an empty binary search tree, such that the resulting tree has height 6, is ____.
Note: The height of a tree with a single node is 0.
Explanation
With 7 nodes, height 6 means every node has at most one child — a straight chain. Inserting into a BST produces a chain exactly when every newly-inserted element is either smaller than ALL previously-inserted elements (attaches as the new leftmost leaf) or larger than all of them (attaches as the new rightmost leaf) — i.e. each insertion is a running min-or-max. After the first element (7 choices, but by symmetry the COUNT of valid sequences doesn't depend on which value starts), each of the remaining n-1=6 insertions independently has exactly 2 valid choices (extend as new min, or extend as new max) among the remaining values — verified against a concrete n=3 enumeration (orders 123,132,312,321 out of 6 total give a chain, matching 2^(3-1)=4). So the count is 2^(n-1) = 2^6 = 64.
Source: GATE CS 2016 (Session 2 / Set B) (2016)
This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.