diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index 1e3ffc236f..20f295a84b 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -78,6 +78,7 @@ | 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [C](./src/561.c) | Easy | | 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [C](./src/617.c) | Easy | | 647 | [Palindromic Substring](https://leetcode.com/problems/palindromic-substrings/) | [C](./src/647.c) | Medium | +| 669 | [Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree/) | [C](./src/669.c) | Medium | | 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [C](./src/674.c) | Easy | | 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [C](./src/700.c) | Easy | | 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [C](./src/701.c) | Medium | diff --git a/leetcode/src/669.c b/leetcode/src/669.c new file mode 100644 index 0000000000..f8842a3463 --- /dev/null +++ b/leetcode/src/669.c @@ -0,0 +1,30 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * struct TreeNode *left; + * struct TreeNode *right; + * }; + */ + + +// Depth-First Search +// Runtime: O(n) +// Space: O(1) +struct TreeNode* trimBST(struct TreeNode* root, int low, int high){ + if (root == NULL){ + return NULL; + } + + if (root->val > high){ + return trimBST(root->left, low, high); + } + + if (root->val < low){ + return trimBST(root->right, low, high); + } + + root->left = trimBST(root->left, low, high); + root->right = trimBST(root->right, low, high); + return root; +}