Simple Java recursion


  • 0
    N
    class Solution {
        public TreeNode trimBST(TreeNode root, int L, int R) {
            if (root == null) {
                return root;
            }
            
            TreeNode left = trimBST(root.left, L, R);
            TreeNode right = trimBST(root.right, L, R);
            
            if (root.val <= R && root.val >= L) {
                root.left = left;
                root.right = right;
                return root;
            }
            else {
                if (root.val > R) {
                    return left;
                }
                else {
                    return right;
                }
            }
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.