zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    Java搭建邮件服务器并发送Excel附件
    Java发送Http带HEADER参数
    MySql 技术内幕 (查询处理和子查询)
    《MySQL技术内幕:SQL编程》笔记
    MySql 技术内幕 (数据类型)
    替换Jar包里文件
    [Python数据分析]新股破板买入,赚钱几率如何?
    一些资料
    sqlval
    IBM CLI 和 ODBC
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10063439.html
Copyright © 2011-2022 走看看