The range of LeetCode binary search tree and

1. Title

Insert picture description here


2. Initial problem solving ideas (JAVA language)

In fact, it is the pre-order traversal of the binary tree + restriction conditions

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

Time complexity: T(N), space complexity O(N)


3. Optimize ideas

Note that it is a binary search tree, that is, the value of the left node must be less than the value of the root node, and the value of the right node must be greater than the value of the root node. When the value of the root node is less than L, we only need to check the root node Traverse the right subtree of the root node. When the value of the root node is greater than R, only the left subtree of the root node needs to be traversed.

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

Time complexity: T(N), space complexity O(N)


Other ideas

There is also a special idea. The middle order traversal of the binary search tree is ordered. We can directly traverse the root node in middle order, then put the eligible points into a List, and finally traverse the List directly to return the result.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    
    

    List<Integer> list = new ArrayList<Integer>();

    public int rangeSumBST(TreeNode root, int L, int R) {
    
    
        getNodes(root,L,R);
        return getListSum(list);
    }

    public void getNodes(TreeNode root, int L, int R){
    
    
        if(root!=null){
    
    
            getNodes(root.left,L,R);
            if(root.val>=L && root.val<=R){
    
    
                list.add(root.val);
            }
            getNodes(root.right,L,R);
        }
    }

    public int getListSum(List<Integer> list){
    
    
        int cnt = 0;
        for(Integer num:list){
    
    
            cnt+=num.intValue();
        }
        return cnt;
    }
    
}

Time complexity: T(N), space complexity O(N), but this idea reduces the memory overhead to a certain extent

Guess you like

Origin blog.csdn.net/weixin_43967679/article/details/108730218