↓Skip to main content
  1. LeetCode/

LeetCode 938: Range Sum of BST

·2 mins· ·
LeetCode Easy Tree Depth-First-Search Binary-Search-Tree Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-26

  • Submissions: Accepted ×3 → Wrong Answer → Runtime Error → Accepted ×2 → Wrong Answer → Accepted

Approach
#

The BST order skips impossible subtrees (Tree Template 3):

  • Node < low: it and its whole left subtree are too small, so look only right.
  • Node > high: look only left.
  • Inside the range: add it and look both ways.

My Attempts
#

In September 2022 one version defined the recursive helper but never called it (and modified the outer sum_ without nonlocal), returning 0. The November rewrite started from low + high, assuming both are values in the tree; it passed the tests, but nothing guarantees that: [9] with low = high = 5 returns 10 instead of 0.

Solution
#

class Solution:
    def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:
        if not root:
            return 0
        if root.val < low:
            return self.rangeSumBST(root.right, low, high)
        if root.val > high:
            return self.rangeSumBST(root.left, low, high)
        return root.val + self.rangeSumBST(root.left, low, high) + self.rangeSumBST(root.right, low, high)

Complexity
#

Time O(n) worst case, space O(h).

Mistakes to Avoid
#

  • Both ends of the range are inclusive.
  • Do not assume low and high are values in the tree.

Interview Explanation
#

I prune with the BST order: below low search only right, above high only left, and inside the range add the node and search both. Worst case O(n).

Related

LeetCode 501: Find Mode in Binary Search Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 501 note: a BST’s in-order is sorted, so equal values are contiguous; count the current run during in-order, resetting the answer on a new maximum and appending on a tie
LeetCode 965: Univalued Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 965 note: every value must equal the root’s; compare values while recursing, not the booleans the recursion returns
LeetCode 993: Cousins in Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 993 note: cousins share a depth but not a parent; record x’s and y’s depth and parent during DFS and compare
LeetCode 897: Increasing Order Search Tree
·1 min
LeetCode Easy Stack Tree Depth-First-Search Binary-Search-Tree
LeetCode 897 note: in-order traversal appending nodes to a right-only chain with left children cleared; keep the dummy separate from the moving tail
LeetCode 872: Leaf-Similar Trees
·1 min
LeetCode Easy Tree Depth-First-Search Binary-Tree
LeetCode 872 note: collect leaves left to right with DFS; BFS collects level by level, which is not left-to-right order
LeetCode 783: Minimum Distance Between BST Nodes
·2 mins
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Search-Tree
LeetCode 783 note: a BST’s in-order is ascending, so the minimum difference is between in-order neighbors; remember the previous value and compare