↓Skip to main content
  1. LeetCode/

LeetCode 889: Construct Binary Tree from Preorder and Postorder Traversal

·2 mins· ·
LeetCode Medium Array Hash-Table Divide-and-Conquer Tree Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-10-29

  • Submissions: Accepted (first try)

Approach
#

Pre-order is “root, left, right” and post-order is “left, right, root”. When a node appears in post-order, its whole subtree is finished.

Read pre-order in order (Tree Template 9):

  • The stack holds the nodes on the current root-to-node path whose subtrees are not finished.
  • Before placing a new node, check whether the stack top equals the value at the post-order pointer. If so, that subtree is complete: pop it and advance the pointer, and repeat.
  • The new node is a child of the stack top: the left child if the top has none yet, otherwise the right child. Then push it.

For a node with a single child, pre-order and post-order cannot tell left from right. Any valid answer is accepted, and this version always uses the left.

My Attempts
#

Accepted on the first try.

Solution
#

class Solution:
    def constructFromPrePost(self, preorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
        root = TreeNode(preorder[0])
        stack = [root]
        j = 0
        for val in preorder[1:]:
            node = TreeNode(val)
            while stack[-1].val == postorder[j]:
                stack.pop()
                j += 1
            if not stack[-1].left:
                stack[-1].left = node
            else:
                stack[-1].right = node
            stack.append(node)
        return root

Recursive alternative: preorder[1] is the left subtree’s root; its position in post-order gives the left subtree’s size, so split both arrays and recurse. With a hash map of post-order positions it is also O(n).

Complexity
#

Time O(n): each node is pushed and popped once. Space O(h).

Mistakes to Avoid
#

  • Pop before attaching the new node, with a while: several subtrees can finish at once.
  • The answer is not unique, so a single-child node needs no special handling.

Interview Explanation
#

A node showing up in post-order means its subtree is done. I read pre-order with a stack of unfinished nodes; for each new node I first pop while the top equals the current post-order value, then attach the new node to the top, left if empty, else right. Each node is pushed and popped once, so it is O(n).

Related

LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal
·2 mins
LeetCode Medium Array Hash-Table Divide-and-Conquer Tree
LeetCode 106 note: the last post-order value is the root; taking roots from the end of the post-order, build the right subtree before the left
LeetCode 105: Construct Binary Tree from Preorder and Inorder Traversal
·3 mins
LeetCode Medium Array Hash-Table Divide-and-Conquer Tree
LeetCode 105 note: the first pre-order value is the root, and its position in the in-order tells the left subtree’s size; look positions up in a dictionary for O(n)
LeetCode 863: All Nodes Distance K in Binary Tree
·2 mins
LeetCode Medium Hash-Table Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 863 note: turn the tree into an undirected graph (adding edges to parents), then BFS k steps from target
LeetCode 652: Find Duplicate Subtrees
·2 mins
LeetCode Medium Hash-Table Tree Depth-First-Search Binary-Tree
LeetCode 652 note: encode every subtree in post-order and report it on its second occurrence; string keys are O(n^2) worst case, (val, left id, right id) ids make it O(n)
LeetCode 314: Binary Tree Vertical Order Traversal
·2 mins
LeetCode Medium Hash-Table Tree Depth-First-Search Breadth-First-Search Sorting Binary-Tree
LeetCode 314 note: BFS with a column index; BFS is what guarantees top-to-bottom within a column and left-to-right within a row
LeetCode 347: Top K Frequent Elements
·1 min
LeetCode Medium Array Hash-Table Divide-and-Conquer Sorting
LeetCode 347 note: count first, then take the k most frequent with a size-k heap or a bucket sort indexed by count