Rebuild Any Binary Tree Matching Preorder and Postorder
Quick Overview
Rebuild Any Binary Tree Matching Preorder and Postorder. Given preorder and postorder traversals of the same binary tree with distinct values, construct and return a tree matching both traversals.
Rebuild Any Binary Tree Matching Preorder and Postorder
Company: Apple
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
Given preorder and postorder traversals of the same binary tree with distinct values, construct and return a tree matching both traversals. If several binary-tree shapes are consistent, any one is accepted. Preserve the native TreeNode return interface; level-order arrays below only display example trees.
```cpp
class Solution {
public:
TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder);
};
```
### Examples
**Example 1:**
```text
Input: preorder = [2,1,3], postorder = [1,3,2]
Output: tree displayed as[2,1,3]
Explanation: Root2 has left child1 and right child3, matching both traversals.
```
**Example 2:**
```text
Input: preorder = [1,2], postorder = [2,1]
Output: tree displayed as[1,2]
Explanation: Making2 the left child of1 works. Making it the right child also gives the same traversals.
```
### Constraints
- `1 <= preorder.length <= 30`.
- `postorder.length == preorder.length`.
- Every value in either array lies between1 and preorder.length, inclusive.
- Values in each traversal are unique.
- Both arrays are guaranteed traversals of the same binary tree.
### Contract considerations
Traversals are guaranteed mutually valid and use distinct values. Return a TreeNode pointer, not an invented array wrapper.
```hint Read the boundary condition
Locate the next preorder root in postorder to find the first child subtree size.
```
### What a Strong Answer Covers
Traversals are guaranteed mutually valid and use distinct values. Return a TreeNode pointer, not an invented array wrapper.
### Follow-up Questions
Optional practice extension, separate from the original task: What additional traversal information would make every valid tree reconstruction unique?
Overview: Rebuild Any Binary Tree Matching Preorder and Postorder. Given preorder and postorder traversals of the same binary tree with distinct values, construct and return a tree matching both traversals.
Rebuild Any Binary Tree Matching Preorder and Postorder
Apple
Jun 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
Given preorder and postorder traversals of the same binary tree with distinct values, construct and return a tree matching both traversals. If several binary-tree shapes are consistent, any one is accepted. Preserve the native TreeNode return interface; level-order arrays below only display example trees.
class Solution {
public:
TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder);
};
Examples
Example 1:
Input: preorder = [2,1,3], postorder = [1,3,2]
Output: tree displayed as[2,1,3]
Explanation: Root2 has left child1 and right child3, matching both traversals.
Example 2:
Input: preorder = [1,2], postorder = [2,1]
Output: tree displayed as[1,2]
Explanation: Making2 the left child of1 works. Making it the right child also gives the same traversals.
Constraints
1 <= preorder.length <= 30
.
postorder.length == preorder.length
.
Every value in either array lies between1 and preorder.length, inclusive.
Values in each traversal are unique.
Both arrays are guaranteed traversals of the same binary tree.
Contract considerations
Traversals are guaranteed mutually valid and use distinct values. Return a TreeNode pointer, not an invented array wrapper.
What a Strong Answer Covers Guidance
Traversals are guaranteed mutually valid and use distinct values. Return a TreeNode pointer, not an invented array wrapper.
Follow-up Questions Guidance
Optional practice extension, separate from the original task: What additional traversal information would make every valid tree reconstruction unique?