列表

详情


938. 二叉搜索树的范围和

给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和。

 

示例 1:

输入:root = [10,5,15,3,7,null,18], low = 7, high = 15
输出:32

示例 2:

输入:root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
输出:23

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: int rangeSumBST(TreeNode* root, int low, int high) { } };

java 解法, 执行用时: 0 ms, 内存消耗: 49.9 MB, 提交时间: 2024-02-26 09:54:54

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

python3 解法, 执行用时: 120 ms, 内存消耗: 24.3 MB, 提交时间: 2024-02-26 09:50:13

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:
        ans = 0
        stack = [root]
        while stack:
            node = stack.pop(0)
            if node == None: continue
            if node.val > high:
                stack.append(node.left)
            elif node.val < low:
                stack.append(node.right)
            else:
                ans += node.val
                stack.append(node.left)
                stack.append(node.right)
            
        return ans

python3 解法, 执行用时: 102 ms, 内存消耗: 24.1 MB, 提交时间: 2024-02-26 09:45:33

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:
        if root == None: return 0
    
        if root.val > high:
            return self.rangeSumBST(root.left, low, high)
        
        if root.val < low:
            return self.rangeSumBST(root.right, low, high)
        
        return root.val + self.rangeSumBST(root.left, low, high) + self.rangeSumBST(root.right, low, high)

golang 解法, 执行用时: 124 ms, 内存消耗: 9.2 MB, 提交时间: 2021-06-04 15:09:36

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func rangeSumBST(root *TreeNode, low int, high int) int {
    if root == nil {
        return 0
    }

    if root.Val > high {
        return rangeSumBST(root.Left, low, high)
    }

    if root.Val < low {
        return rangeSumBST(root.Right, low, high)
    }

    return root.Val + rangeSumBST(root.Left, low, high) + rangeSumBST(root.Right, low, high)

}

golang 解法, 执行用时: 108 ms, 内存消耗: 9 MB, 提交时间: 2021-06-04 15:05:05

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func rangeSumBST(root *TreeNode, low int, high int) int {
    ans := 0
    stack := []*TreeNode{root}
    for len(stack) > 0 {
        node := stack[0]
        stack = stack[1:]
        if node == nil {
            continue
        }
        if node.Val > high {
            stack = append(stack, node.Left)
        } else if node.Val < low {
            stack = append(stack, node.Right)
        } else {
            ans += node.Val
            stack = append(stack, node.Left, node.Right)
        }
    }
    return ans

}

上一题