Skip to content

Commit ca46ddf

Browse files
authored
Create 669. Trim a Binary Search Tree.cpp
1 parent e1e60b5 commit ca46ddf

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
TreeNode* trimBST(TreeNode* root, int L, int R) {
13+
while(root && (root->val < L || root->val > R)) root = root->val < L ? root->right : root->left;
14+
if(!root) return NULL;
15+
root->left = trimBST(root->left, L, R);
16+
root->right = trimBST(root->right, L, R);
17+
return root;
18+
}
19+
};

0 commit comments

Comments
 (0)