KatInterview

Recover the Preorder of a BST from Its Postorder Listing

Trees & GraphsAuto-graded · 26 test cases
MediumAuto-gradedTrees & Graphsbinary-search-treemonotonic-stacktree-traversal

Last updated

Turn the postorder listing of a distinct-valued BST into its preorder listing in linear time, including chain-shaped trees, using a monotonic stack over the reversed input.

A binary search tree (BST) stores distinct integers. Each node has at most one left child and at most one right child, and for every node x:

  • every value in the left subtree of x is strictly smaller than x;
  • every value in the right subtree of x is strictly larger than x.

An empty subtree contributes nothing to a traversal.

Traversal orders

For a (sub)tree rooted at node x, the two listings are defined recursively:

  • Postorder: the postorder of the left subtree, then the postorder of the right subtree, then x.
  • Preorder: x, then the preorder of the left subtree, then the preorder of the right subtree.

In both orders the left subtree always comes before the right subtree. In postorder the last element of the whole listing is the root of the tree.

Task

You are given postorder, the postorder listing of some BST with distinct values. Because the values are distinct and the tree obeys the BST rule, exactly one tree produces this listing. Rebuild that tree (conceptually) and return its preorder listing as a list of integers.

Write bst_preorder_from_postorder(postorder), which takes the postorder list and returns the preorder list. The returned list has the same length as the input and contains the same values, each exactly once, in preorder. The order of the returned elements matters, and the input list should be treated as read-only.

Rules

  • The input is always a valid postorder listing of some BST with distinct values. You do not need to validate it.
  • A one-element input is a tree with a single node, so the output is that same one-element list.
  • Trees can be completely lopsided. For example, every node may have only a right child, or only a left child, so the height can equal the number of values. Your solution must handle this without exceeding Python's recursion limit.
  • The solution must run in $O(n)$ time, where $n = $ len(postorder).

Constraints

  • 1 <= len(postorder) <= 10^4
  • -10^9 <= postorder[i] <= 10^9
  • All values in postorder are distinct.
  • postorder is a valid BST postorder listing.
  • Required time complexity: $O(n)$.

Examples

Example 1

Input: bst_preorder_from_postorder([1, 3, 2, 5, 7, 6, 4])

Output: [4, 2, 1, 3, 6, 5, 7]

Example 2

Input: bst_preorder_from_postorder([2, 4, 3, 8, 7, 5])

Output: [5, 3, 2, 4, 7, 8]

Example 3

Input: bst_preorder_from_postorder([3, 2, 1])

Output: [1, 2, 3]

Hints

Hint 1

Postorder is left, right, node. Read it from right to left and you get node, right subtree, left subtree, which is a preorder that visits the right side first.

Hint 2

While scanning in reverse, keep a stack of nodes that may still receive a left child, strictly increasing from bottom to top. Compare each new value with the top of the stack to decide where it attaches.

Hint 3

If the new value is larger than the top, it is the top's right child. Otherwise pop everything larger than it; the last node popped is its parent and the value becomes that node's left child. Produce the final preorder with an iterative traversal.