Let's make this one less mysterious. For 1008. Construct Binary Search Tree from Preorder Traversal, the solution in this repository is mainly a two pointers solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. 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, sliding window.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are bstFromPreorder, rbstFromPreorder.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- 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:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
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: 4 ms, faster than 83.62% of C++ online submissions for Construct Binary Search Tree from Preorder Traversal.
02//Memory Usage: 8.9 MB, less than 100.00% of C++ online submissions for Construct Binary Search Tree from Preorder Traversal.
03
04/**
05 * Definition for a binary tree node.
06 * struct TreeNode {
07 * int val;
08 * TreeNode *left;
09 * TreeNode *right;
10 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
11 * };
12 */
13class Solution {
14public:
15
16 TreeNode* bstFromPreorder(vector<int>& preorder) {
17 if(preorder.size() == 0) return NULL;
18 TreeNode *root, *pre, *cur;
19 root = new TreeNode(preorder[0]);
20
21 for(int i = 1; i < preorder.size(); i++){
22 int p = preorder[i];
23 cur = root;
24 while(cur){
25 pre = cur;
26 if(p < cur->val){
27 cur = cur->left;
28 }else if(p >= cur->val){
29 cur = cur->right;
30 }
31 }
32 if(p < pre->val){
33 pre->left = new TreeNode(p);
34 }else if(p >= pre->val){
35 pre->right = new TreeNode(p);
36 }
37 }
38 return root;
39 }
40};
41
42//Recursive
43//Runtime: 4 ms, faster than 86.93% of C++ online submissions for Construct Binary Search Tree from Preorder Traversal.
44//Memory Usage: 11.6 MB, less than 28.57% of C++ online submissions for Construct Binary Search Tree from Preorder Traversal.
45/**
46 * Definition for a binary tree node.
47 * struct TreeNode {
48 * int val;
49 * TreeNode *left;
50 * TreeNode *right;
51 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
52 * };
53 */
54class Solution {
55public:
56 TreeNode* rbstFromPreorder(vector<int>& preorder, int l, int r){
57 // cout << l << " " << r << endl;
58 if(l > r) return nullptr;
59 //[l, r]: inclusive
60 TreeNode* node = new TreeNode(preorder[l]);
61
62 //the smallest number > x
63 auto it = upper_bound(preorder.begin()+l+1, preorder.begin()+r+1, preorder[l]);
64
65 node->left = rbstFromPreorder(preorder, l+1, it-preorder.begin()-1);
66 node->right = rbstFromPreorder(preorder, it-preorder.begin(), r);
67
68 return node;
69 };
70
71 TreeNode* bstFromPreorder(vector<int>& preorder) {
72 return rbstFromPreorder(preorder, 0, preorder.size()-1);
73 }
74};
Cost