669. Trim a Binary Search Tree 發表於 2023-02-13 | 分類於 leetcode problemsolution1234567891011121314151617181920212223242526/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */class Solution {public: TreeNode* trimBST(TreeNode* root, int L, int R) { //只是將Node的指向進行的更改,而不是實際刪除Node if (root == nullptr) return nullptr; // Node 太小,那返回他的右子樹,右子樹比他大 if (root->val < L) return trimBST(root->right, L, R); if (root->val > R) return trimBST(root->left, L, R); // 遞迴 root->left = trimBST(root->left, L, R); root->right = trimBST(root->right, L, R); return root; }}; analysis time complexity O(n) space complexity O(n)