// Just a naive idea (good for java). Hadn't freed memory yet.

TreeNode* trimBST(TreeNode* root, int L, int R) {

return !root ? NULL : (root->left = trimBST(root->left, L, R), root->right = trimBST(root->right, L, R), (L<=root->val&&root->val<=R) ? root : (root->val > R ? root->left : root->right));

}