现在对递归算法的理解就是返回的是对当前点的处理结果。如果左边不行就返回对右子树处理的结果,如果右边不行就返回对左子树处理的结果,如果两边都可以就对左右分别处理,再返回该节点。
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root == nullptr) return nullptr;
if(root->val < low) {
return trimBST(root->right, low, high);
} else if(root->val > high) {
return trimBST(root->left, low, high);
} else {
root->left = trimBST(root->left, low, high);
root->right = trimBST(root->right, low, high);
return root;
}
}