Given a range of [start,end] write a program to trim branch of BST outside the range.

     5
   /  \
  3    10
 / \   / \
1   4 7   8
Give a BST and range [3,8], trim the above tree
     5
   /  \
  3    7
        \
         8

One way to approach these problem is to start inorder traversal, if the value of node is outside the range ignore the node and traverse right or left of the node depending on whether the value lies to left or right side.

public TreeNode trimBST(TreeNode root, int L, int R) {
        if (root == null) return root;
        if (root.val > R) return trimBST(root.left, L, R);
        if (root.val < L) return trimBST(root.right, L, R);

        root.left = trimBST(root.left, L, R);
        root.right = trimBST(root.right, L, R);
        return root;
    }