• 938. Range Sum of BST


    Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).

    The binary search tree is guaranteed to have unique values.

    Example 1:

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

    Example 2:

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

    Note:

    1. The number of nodes in the tree is at most 10000.
    2. The final answer is guaranteed to be less than 2^31.
     

    Approach #1: C++. [recursive]

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int rangeSumBST(TreeNode* root, int L, int R) {
            ans = 0;
            dfs(root, L, R);
            return ans;
        }
        
    private:
        int ans;
        
        void dfs(TreeNode* root, int L, int R) {
            if (root != NULL) {
                if (L <= root->val && root->val <= R) {
                    ans += root->val;
                }
                if (L < root->val) {
                    dfs(root->left, L, R);
                }
                if (R > root->val) {
                    dfs(root->right, L, R);
                }
            }
        }
    };
    

      

    Approach #2: Java. [Iterative]

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

      

    Approach #3: Python.

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def rangeSumBST(self, root, L, R):
            """
            :type root: TreeNode
            :type L: int
            :type R: int
            :rtype: int
            """
            ans = 0
            stack = [root]
            while stack:
                node = stack.pop()
                if node:
                    if L <= node.val <= R:
                        ans += node.val
                    if L < node.val:
                        stack.append(node.left)
                    if R > node.val:
                        stack.append(node.right)
                    
            return ans
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    常用正则表达式
    偶得
    监控文件夹里面文件修改的小程序
    使用Windows服务发布WCF服务
    查看wcf服务中方法测试客户端
    twitter注册使用指南
    打包工具使用下载
    c#多线程编程
    请确保此文件可访问并且是一个有效的程序集或COM组件
    添加Service Reference, 无法为服务生成代码错误的解决办法
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10063439.html
Copyright © 2020-2023  润新知