Skip to content

Commit b37bf7f

Browse files
feat: add Trim a Binary Search Tree LeetCode problem (#1156)
1 parent 794ec12 commit b37bf7f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

leetcode/DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [C](./src/561.c) | Easy |
8181
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [C](./src/617.c) | Easy |
8282
| 647 | [Palindromic Substring](https://leetcode.com/problems/palindromic-substrings/) | [C](./src/647.c) | Medium |
83+
| 669 | [Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree/) | [C](./src/669.c) | Medium |
8384
| 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [C](./src/674.c) | Easy |
8485
| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [C](./src/700.c) | Easy |
8586
| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [C](./src/701.c) | Medium |

leetcode/src/669.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
11+
// Depth-First Search
12+
// Runtime: O(n)
13+
// Space: O(1)
14+
struct TreeNode* trimBST(struct TreeNode* root, int low, int high){
15+
if (root == NULL){
16+
return NULL;
17+
}
18+
19+
if (root->val > high){
20+
return trimBST(root->left, low, high);
21+
}
22+
23+
if (root->val < low){
24+
return trimBST(root->right, low, high);
25+
}
26+
27+
root->left = trimBST(root->left, low, high);
28+
root->right = trimBST(root->right, low, high);
29+
return root;
30+
}

0 commit comments

Comments
 (0)