Java Neat Recursive Solution

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

Log in to reply

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