mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
feat: add Trim a Binary Search Tree LeetCode problem (#1156)
This commit is contained in:
parent
794ec129ae
commit
b37bf7f6b9
@ -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
30
leetcode/src/669.c
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user