08. Construct Tree from Preorder & Postorder
β GFG solution to the Construct Tree from Preorder & Postorder problem: build a full binary tree from preorder and postorder traversals using recursive approach with optimized lookups. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given two arrays pre[]
and post[]
that represent the preorder and postorder traversals of a full binary tree, your task is to construct the binary tree and return its root.
A full binary tree is a binary tree where every node has either 0 or 2 children. The preorder and postorder traversals contain unique values, and every value present in the preorder traversal is also found in the postorder traversal.
π Examples
Example 1
Input: pre[] = [1, 2, 4, 8, 9, 5, 3, 6, 7], post[] = [8, 9, 4, 5, 2, 6, 7, 3, 1]
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Explanation: The tree structure is:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
Example 2
Input: pre[] = [1, 2, 4, 5, 3, 6, 7], post[] = [4, 5, 2, 6, 7, 3, 1]
Output: [1, 2, 3, 4, 5, 6, 7]
Explanation: The tree structure is:
1
/ \
2 3
/ \ / \
4 5 6 7
π Constraints
$1 \le \text{number of nodes} \le 10^3$
$1 \le \text{pre}[i], \text{post}[i] \le 10^4$
β
My Approach
The optimal approach uses recursive tree construction with postorder boundary tracking:
Recursive Construction with Postorder Search
Understanding Traversal Properties:
In preorder: Root β Left Subtree β Right Subtree
In postorder: Left Subtree β Right Subtree β Root
The first element in preorder is always the root
The last element in postorder is always the root
Key Insight:
After identifying the root from preorder, the next element is the root of the left subtree
Find this left subtree root in postorder to determine the boundary between left and right subtrees
Algorithm Steps:
Use a global index pointer to track current position in preorder array
Create root node with current preorder element and increment index
If subtree is not a leaf (l β r) and more nodes exist:
Find the position of next preorder element (left child) in postorder within current range
Recursively build left subtree from l to pos
Recursively build right subtree from pos+1 to r-1
Return the constructed root
Base Case:
When left boundary equals right boundary (l == r), we have a leaf node
When index reaches end of preorder array, no more nodes to process
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(nΒ²), where n is the number of nodes in the tree. In the worst case, for each node we perform a linear search in the postorder array to find the left child position, resulting in O(n) operations per node.
Expected Auxiliary Space Complexity: O(h), where h is the height of the tree. This space is used by the recursion call stack. In the worst case of a skewed tree, h can be O(n), but for a balanced full binary tree, h = O(log n).
π§βπ» Code (C++)
class Solution {
public:
int idx = 0;
Node* constructTree(vector<int>& pre, vector<int>& post) {
return build(pre, post, 0, post.size() - 1);
}
Node* build(vector<int>& pre, vector<int>& post, int l, int r) {
Node* root = new Node(pre[idx++]);
if (l != r && idx < pre.size()) {
int pos = l;
while (post[pos] != pre[idx]) pos++;
root->left = build(pre, post, l, pos);
root->right = build(pre, post, pos + 1, r - 1);
}
return root;
}
};
β Code (Java)
class Solution {
int idx = 0;
public Node constructTree(int[] pre, int[] post) {
return build(pre, post, 0, post.length - 1);
}
Node build(int[] pre, int[] post, int l, int r) {
Node root = new Node(pre[idx++]);
if (l != r && idx < pre.length) {
int pos = l;
while (post[pos] != pre[idx]) pos++;
root.left = build(pre, post, l, pos);
root.right = build(pre, post, pos + 1, r - 1);
}
return root;
}
}
π Code (Python)
class Solution:
def constructTree(self, pre, post):
self.idx = 0
return self.build(pre, post, 0, len(post) - 1)
def build(self, pre, post, l, r):
root = Node(pre[self.idx])
self.idx += 1
if l != r and self.idx < len(pre):
pos = post.index(pre[self.idx], l, r + 1)
root.left = self.build(pre, post, l, pos)
root.right = self.build(pre, post, pos + 1, r - 1)
return root
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated