This problem looks busy at first, but the accepted solution is built around one steady invariant. For 889. Construct Binary Tree from Preorder and Postorder Traversal, the solution in this repository is mainly a two pointers solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are constructFromPrePostR, constructFromPrePost, make.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 12 ms, faster than 72.49% of C++ online submissions for Construct Binary Tree from Preorder and Postorder Traversal.
02//Memory Usage: 15 MB, less than 16.67% of C++ online submissions for Construct Binary Tree from Preorder and Postorder Traversal.
03/**
04 * Definition for a binary tree node.
05 * struct TreeNode {
06 * int val;
07 * TreeNode *left;
08 * TreeNode *right;
09 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10 * };
11 */
12class Solution {
13public:
14 TreeNode* constructFromPrePostR(vector<int>& pre, vector<int>& post, int preStart, int preEnd, int postStart, int postEnd) {
15 // cout << "[" << preStart << ", " << preEnd << "]" << "[" << postStart << ", " << postEnd << "]" << endl;
16 assert(pre[preStart] == post[postEnd]);
17 TreeNode* root = new TreeNode(pre[preStart]);
18 int N = preEnd - preStart + 1;
19
20 if(N > 1){
21 //pre[preStart+1]: left subtree's root
22 int postLeftEnd = find(post.begin()+postStart, post.end()+postEnd+1, pre[preStart+1]) - post.begin();
23 //left subtree's size: postLeftEnd - postStart + 1
24 int leftSize = postLeftEnd - postStart + 1;
25 int rightSize = N - leftSize - 1;
26 // cout << "postLeftEnd: " << postLeftEnd << endl;
27 // cout << "size: " << leftSize << ", " << rightSize << endl;
28 if(leftSize >= 1){
29 // cout << "left" << endl;
30 root->left = constructFromPrePostR(pre, post, preStart+1, preStart+1+leftSize-1, postStart, postLeftEnd);
31 }
32
33 if(rightSize >= 1){
34 // cout << "right" << endl;
35 // cout << "preStart: " << preStart << ", leftSize: " << leftSize << ", preEnd: " << preEnd << ", postLeftEnd: " << postLeftEnd << ", postEnd: " << postEnd << endl;
36 root->right = constructFromPrePostR(pre, post, preStart+1+leftSize, preEnd, postLeftEnd+1, postEnd-1);
37 }
38 }
39
40 return root;
41 }
42
43 TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
44 int N = pre.size();
45 assert(pre[0] == post[N-1]);
46 TreeNode* root = new TreeNode(pre[0]);
47
48 if(N > 1){
49 int postLeftEnd = find(post.begin(), post.end(), pre[1]) - post.begin();
50 int leftSize = postLeftEnd + 1;
51 int rightSize = N - leftSize - 1;
52 // cout << "postLeftEnd: " << postLeftEnd << endl;
53 // cout << "postLeftEnd: " << find(post.begin(), post.end(), 2) - post.begin() << endl;
54 // cout << "postLeftEnd: " << (find(post.begin(), post.end(), 2) == post.end()) << endl;
55 // cout << "post: " << post.size() << endl;
56 //left subtree's size is postSplit+1
57 //preEnd = 1 + leftsubtree's size(0-based)
58
59 if(leftSize){
60 // cout << "left" << endl;
61 root->left = constructFromPrePostR(pre, post, 1, 1+leftSize-1, 0, postLeftEnd);
62 }
63
64 if(rightSize){
65 //preStart: next element of preEnd in left subtree
66 // cout << "right" << endl;
67 root->right = constructFromPrePostR(pre, post, leftSize+1, N-1, postLeftEnd+1, N-2);
68 }
69 }
70
71 return root;
72 }
73};
74
75//Approach 2: Recursion (Space Saving Variant)
76//time: O(N^2), space: O(N)
77//Runtime: 12 ms, faster than 72.49% of C++ online submissions for Construct Binary Tree from Preorder and Postorder Traversal.
78//Memory Usage: 15.1 MB, less than 16.67% of C++ online submissions for Construct Binary Tree from Preorder and Postorder Traversal.
79
80/**
81 * Definition for a binary tree node.
82 * struct TreeNode {
83 * int val;
84 * TreeNode *left;
85 * TreeNode *right;
86 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
87 * };
88 */
89class Solution {
90public:
91 vector<int> pre, post;
92
93 TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
94 this->pre = pre;
95 this->post = post;
96 return make(0, 0, pre.size());
97 }
98
99 TreeNode* make(int preStart, int postStart, int N){
100 if(N == 0) return NULL;
101 TreeNode* root = new TreeNode(pre[preStart]);
102 if(N == 1) return root;
103
104 //left subtree's size
105 int L = find(post.begin()+postStart, post.begin()+postStart+N, pre[preStart+1]) - (post.begin()+postStart)+1;
106
107 root->left = make(preStart+1, postStart, L);
108 root->right = make(preStart+1+L, postStart+L,N-L-1);
109 return root;
110 }
111};
Cost