feat: add Trim a Binary Search Tree LeetCode problem (#1156)

This commit is contained in:
Alexander Pantyukhin 2022-12-02 09:11:58 +04:00 committed by GitHub
parent 794ec129ae
commit b37bf7f6b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -80,6 +80,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 |

30
leetcode/src/669.c Normal file
View File

@ -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;
}