Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 979d1fb

Browse filesBrowse files
authored
TC: O(N)
1 parent 90a4919 commit 979d1fb
Copy full SHA for 979d1fb

File tree

Expand file treeCollapse file tree

1 file changed

+28
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+28
-0
lines changed
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* trimBST(TreeNode* root, int low, int high) {
15+
16+
if (root == NULL)
17+
return root;
18+
else if (root->val > high)
19+
return trimBST(root->left, low, high);
20+
else if (root->val < low)
21+
return trimBST(root->right, low, high);
22+
else {
23+
root->left = trimBST(root->left, low, high);
24+
root->right = trimBST(root->right, low, high);
25+
return root;
26+
}
27+
}
28+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.