> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/tree/bst/669.-trim-a-binary-search-tree.md).

# 669. Trim a Binary Search Tree

preorder, postorder 都可以做

````java
```java
class Solution {
    public TreeNode trimBST(TreeNode node, int low, int high) {
        if (node == null) {
            return null;
        }
        node.left = trimBST(node.left, low, high); // 為了接上新結果, 要 = 回去
        node.right = trimBST(node.right, low, high);

        if (node.val < low) { // 放棄左邊, 接右邊就可以
            return node.right;
        }
        if (node.val > high) { // 放棄右邊, 接左邊就可以
            return node.left;
        }
        
        return node; // 在範圍內的值, 就不動回傳
    }
}
```
````
