938. 二叉搜索树的范围和 【二叉搜索树:左子树任一节点的值 < 根节点 < 右子树任意节点的值】

52 阅读2分钟

938. 二叉搜索树的范围和

# 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:
        res = 0
        q = deque([root])
        while q:
            node = q.popleft()
            if low <= node.val <= high:
                res += node.val 
            if node.left:
                q.append(node.left)
            if node.right:
                q.append(node.right)

        return res  

改进:

注意二叉搜索树的特点: 左子树任一节点的值 < 根节点 < 右子树任意节点的值

# 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:
        res = 0
        q = deque([root])
        while q:
            node = q.popleft()
            if low <= node.val <= high:
                res += node.val 
            if node.left and node.val > low:  # 注意 若是 等于了,也无需再看 左子树了,因为是严格递增
                q.append(node.left)
            if node.right and node.val < high: # 右子树 有可能 有符合条件的结点
                q.append(node.right)

        return res  

image.png

DFS 写法

# 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 not root:
            return 0 
        x = root.val 
        if x > high:
            return self.rangeSumBST(root.left, low, high)  # 只需 递归 左子树
        if x < low:
            return self.rangeSumBST(root.right, low, high)  # 只需递归 右子树

        return x + self.rangeSumBST(root.left, low, high) + self.rangeSumBST(root.right, low, high)
# 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 not root:
            return 0 

        x = root.val
        res = x if low <= x <= high else 0 
        if  x > low:
            res += self.rangeSumBST(root.left, low, high)  # 左子树 有可能 有符合条件的结点
        if x < high:
            res += self.rangeSumBST(root.right, low, high) # 右子树 有可能 有符合条件的结点

        return res 

C++

BFS

/**
 * 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) {
        int res = 0;
        queue<TreeNode*> q({root});
        while (!q.empty()){
            TreeNode* node = q.front(); q.pop();
            if (node->val >= low && node->val <= high){
                res += node->val;
            }
            
            if (node->left != nullptr && node->val > low){
                q.push(node->left);
            }
            if (node->right != nullptr && node->val < high){
                q.push(node->right);
            }
        }
        return res;
    }
};

DFS

/**
 * 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) {
        if (root == nullptr){
            return 0;
        }
        int x = root->val;
        if (x > high){
            return rangeSumBST(root->left, low, high);
        }
        if (x < low){
            return rangeSumBST(root->right, low, high);
        }
        return x + rangeSumBST(root->left, low, high) +
                    rangeSumBST(root->right, low, high);
    }
};