Leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal
Given two integer arrays, preorder
and postorder
where preorder
is the preorder traversal of a binary tree and postorder
is the postorder traversal of the same tree, construct and return the binary tree.
preorder
and postorder
will have distinct integer values.Example:
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] // in the form of an in-order traversal for confirmation
Great, let’s move on to the strategy to solve this problem.
Root -> Left Subtree -> Right Subtree
.Left Subtree -> Right Subtree -> Root
.preorder
to identify the root of the tree.preorder
will indicate the root of the left subtree.postorder
array to determine the boundary of the left subtree in both arrays.nullptr
.Here’s how to implement this solution in C++:
#include <vector>
#include <unordered_map>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
public:
TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {
unordered_map<int, int> postorder_map;
for (int i = 0; i < postorder.size(); ++i) {
postorder_map[postorder[i]] = i;
}
return construct(preorder, 0, preorder.size()-1, postorder_map, 0, postorder.size()-1);
}
private:
TreeNode* construct(const vector<int>& preorder, int pre_start, int pre_end,
const unordered_map<int, int>& postorder_map, int post_start, int post_end) {
if (pre_start > pre_end || post_start > post_end) {
return nullptr;
}
TreeNode* root = new TreeNode(preorder[pre_start]);
if (pre_start == pre_end) { // No more children
return root;
}
int left_root_val = preorder[pre_start + 1];
int left_subtree_size = postorder_map.at(left_root_val) - post_start + 1;
root->left = construct(preorder, pre_start + 1, pre_start + left_subtree_size, postorder_map, post_start, post_start + left_subtree_size - 1);
root->right = construct(preorder, pre_start + left_subtree_size + 1, pre_end, postorder_map, post_start + left_subtree_size, post_end - 1);
return root;
}
};
The time complexity analysis of this algorithm is as follows:
Thus, the overall time complexity is O(n), where n is the number of nodes in the tree. Each node is processed in constant time during the construction of the tree.
This solution is efficient and handles the problem constraints well.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?