Skip to content

889. Construct Binary Tree from Preorder and Postorder Traversal #1351

@mah-shamim

Description

@mah-shamim

Discussed in #1350

Originally posted by mah-shamim February 23, 2025
Topics: Array, Hash Table, Divide and Conquer, Tree, Binary Tree

Given two integer arrays, preorder and postorder where preorder is the preorder traversal of a binary tree of distinct values and postorder is the postorder traversal of the same tree, reconstruct and return the binary tree.

If there exist multiple answers, you can return any of them.

Example 1:

lc-prepost

  • Input: preorder = [1,2,4,5,3,6,7], postorder = [4,5,2,6,7,3,1]
  • Output: [1,2,3,4,5,6,7]

Example 2:

  • Input: preorder = [1], postorder = [1]
  • Output: [1]

Constraints:

  • 1 <= preorder.length <= 30
  • 1 <= preorder[i] <= preorder.length
  • All the values of preorder are unique.
  • postorder.length == preorder.length
  • 1 <= postorder[i] <= postorder.length
  • All the values of postorder are unique.
  • It is guaranteed that preorder and postorder are the preorder traversal and postorder traversal of the same binary tree.

Metadata

Metadata

Assignees

Labels

mediumDifficultyquestionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions