Seeking binary search tree node value and range within LeetCode.938- (Range Sum of BST)

This is the first book of pleasure and delight 359 update, the first 386 Pian original

01 questions and look ready

LeetCode algorithm introduced today is the question of Easy level 221 title (overall title number is 938). A root node of a given binary search tree, the node returns the value of the sum value of all the nodes between the [L, R] of. Node binary search tree unique value. E.g:

Input: root = [10,5,15,3,7, null, 18], L = 7, R = 15
Output: 32

Input: root = [10,5,15,3,7,13,18,1, null, 6], L = 6, R = 10
Output: 23

note:

  • The number of nodes in the tree up to 10,000.

  • The final answer is to ensure that less than 2 ^ 31.

02 The first solution

Since it is a binary search tree, then we select the node traversal in preorder fashion, so that the most direct, stored List, and then traversing Listthe node value of the node is equal to a value greater than Lor less Raccumulates, and finally return sumIt can be.

/**
 * 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) {
        List<Integer> list = new ArrayList<Integer>();
        helper(root, list);
        int sum = 0;
        for (Integer num : list) {
            if (num >= L && num <= R) {
                sum += num;
            }
        }
        return sum;
    }

    public void helper(TreeNode root, List<Integer> list){
        if (root == null) {
            return ;
        }
        helper(root.left, list);
        list.add(root.val);
        helper(root.right, list);
    }
}


03 The second solution

For the first solution, we can also use an iterative approach to achieve, with the stack .

/**
 * 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) {
        List<Integer> list = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode tem = stack.pop();
            if (tem != null) {
                list.add(tem.val);
            }
            if (tem != null && tem.left != null) {
                stack.push(tem.left);
            }
            if (tem != null && tem.right != null) {
                stack.push(tem.right);
            }
        }
        int sum = 0;
        for (Integer num : list) {
            if (num >= L && num <= R) {
                sum += num;
            }
        }
        return sum;
    }
}


04 A third solution

In fact, we do not have to save up the value of the node and then centrally, directly at the node traversal time, will be [L,R]the node value in the range of accumulation can finally return sum. This solution is an iterative way.

/**
 * 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) {
        int sum = 0;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode tem = stack.pop();
            if (tem != null) {
                if (tem.val >= L && tem.val <= R) {
                    sum += tem.val;
                }
            }
            if (tem != null && tem.left != null) {
                stack.push(tem.left);
            }
            if (tem != null && tem.right != null) {
                stack.push(tem.right);
            }
        }
        return sum;
    }
}


05 The fourth solution

For the third solution, we can also use a recursive manner. Define a global variable sum, inorder traversal still use manner, in a [L,R]node within a range of values accumulated, and finally returns sum.

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

    private Integer sum = 0;

    public int rangeSumBST(TreeNode root, int L, int R) {
        if (root == null) {
            return sum;
        }        
        rangeSumBST(root.left, L, R);
        if (root.val >= L && root.val <= R) {
            sum += root.val;
        }
        rangeSumBST(root.right, L, R);
        return sum;
    }
}


06 fifth Solution

For the fourth solution, we can not use global variables, using a binary search tree node values are arranged according to the size of the properties left and right of the root, if the current node value than the Lsmall, go out looking for the right, if more than Rbig, go out on the left to find , final summation.

/**
 * 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);
        }
        return root.val + rangeSumBST(root.left, L, R) + 
                rangeSumBST(root.right, L, R);
    }
}


07 Summary

Thematic algorithm has been continuously day and more than seven months , the algorithm of feature articles 227 + articles, public Number dialog box reply [ data structures and algorithms ], [ algorithm ], [ data structures either] in a keyword, to obtain a series of articles Collection .

That's all, if you have any good solution ideas, suggestions or other issues, you can exchange comments below, thumbs up, message forwarding and support is the greatest reward for me!

Guess you like

Origin www.cnblogs.com/xiaochuan94/p/11056503.html