-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 20 ms (61.23%), Space: 24 MB (7.08%) - LeetHub
- Loading branch information
1 parent
9599fad
commit 69a8f3a
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
669-trim-a-binary-search-tree/669-trim-a-binary-search-tree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |