Let's make this one less mysterious. For 669. Trim a Binary Search Tree, 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 is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are trimBST.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- 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:
- Space: O(N).
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.
03
04Example 1:
05Input:
06 1
07 / \
08 0 2
09
10 L = 1
11 R = 2
12
13Output:
14 1
15 \
16 2
17Example 2:
18Input:
19 3
20 / \
21 0 4
22 \
23 2
24 /
25 1
26
27 L = 1
28 R = 3
29
30Output:
31 3
32 /
33 2
34 /
35 1
36 **/
37
38/**
39Complexity Analysis
40
41Time Complexity:
42O(N), where N is the total number of nodes in the given tree.
43We visit each node at most once.
44
45Space Complexity: O(N).
46Even though we don't explicitly use any additional memory,
47the call stack of our recursion could be as large as the number of nodes in the worst case.
48**/
49
50//Runtime: 20 ms, faster than 99.78% of C++ online submissions for Trim a Binary Search Tree.
51//Memory Usage: 13 MB, less than 100.00% of C++ online submissions for Trim a Binary Search Tree.
52/**
53 * Definition for a binary tree node.
54 * struct TreeNode {
55 * int val;
56 * TreeNode *left;
57 * TreeNode *right;
58 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
59 * };
60 */
61class Solution {
62public:
63 //https://www.geeksforgeeks.org/remove-bst-keys-outside-the-given-range/
64 TreeNode* trimBST(TreeNode* root, int L, int R) {
65 if(root==NULL){
66 return NULL;
67 }
68
69 root->left = trimBST(root->left, L, R);
70 root->right = trimBST(root->right, L, R);
71
72 if(root->val < L){
73 TreeNode* newroot = root->right;
74 //now that the left subtree falls in [L,R] and root->val < L
75 //, it can be inferred that root doesn't have left subtree
76 //, so here we only do with right subtree
77 // delete root;
78 root = NULL;
79 return newroot;
80 }
81
82 if(root->val > R){
83 TreeNode* newroot = root->left;
84 // delete root;
85 root = NULL;
86 return newroot;
87 }
88
89 return root;
90 }
91};
Cost