/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode trimBST(TreeNode root, int L, int R) { return dfs(root, L, R); }
public TreeNode dfs(TreeNode root, int L, int R) { if (root == null) return root; if (root.val > R) return dfs(root.left, L, R); if (root.val < L) return dfs(root.right, L, R);