Skip to content

Commit

Permalink
Time: 20 ms (61.23%), Space: 24 MB (7.08%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
gkapelyushok committed Apr 15, 2022
1 parent 9599fad commit 69a8f3a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 669-trim-a-binary-search-tree/669-trim-a-binary-search-tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if (!root) {
return nullptr;
}
if (root->val < low) {
return trimBST(root->right, low, high);
}
if (root->val > high) {
return trimBST(root->left, low, high);
}
root->left = trimBST(root->left, low, high);
root->right = trimBST(root->right, low, high);
return root;
}
};

0 comments on commit 69a8f3a

Please sign in to comment.