Dataford
Interview Guides
Upgrade
All questions/Coding/Range Sum in Binary Tree

Range Sum in Binary Tree

Medium
Coding
Asked at 1 company1TreesRecursionMath
Also asked at
Meta

Problem

Problem

In a Meta interview setting, you are given the root of a binary tree and two integers low and high. Return the sum of all node values v such that low <= v <= high.

Assume the tree is a binary search tree (BST), which allows you to skip subtrees that cannot contain valid values.

Formal Specification

  • Input: root as a binary tree node (or null), and integers low, high
  • Output: An integer representing the sum of all node values in the inclusive range [low, high]

Examples

Example 1

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

Explanation: The values in range are 7, 10, and 15, so the sum is 32.

Example 2

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

Explanation: The values in range are 6, 7, and 10, so the sum is 23.

Constraints

  • The number of nodes is in the range [0, 2 * 10^4]
  • 1 <= Node.val <= 10^5
  • 1 <= low <= high <= 10^5
  • The input tree satisfies BST ordering rules

A correct solution should use the BST property to avoid unnecessary traversal when possible.

Examples

Example 1
Inputroot = [10,5,15,3,7,null,18], low = 7, high = 15Output32WhyThe node values within the inclusive range are 7, 10, and 15.
Example 2
Inputroot = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10Output23WhyThe valid node values are 6, 7, and 10, so the sum is 23.

Constraints

  • The number of nodes is in the range [0, 2 * 10^4]
  • 1 <= Node.val <= 10^5
  • 1 <= low <= high <= 10^5
  • The input tree is a valid binary search tree

Function Signature

def range_sum_bst(root, low, high):

Problem

Problem

In a Meta interview setting, you are given the root of a binary tree and two integers low and high. Return the sum of all node values v such that low <= v <= high.

Assume the tree is a binary search tree (BST), which allows you to skip subtrees that cannot contain valid values.

Formal Specification

  • Input: root as a binary tree node (or null), and integers low, high
  • Output: An integer representing the sum of all node values in the inclusive range [low, high]

Examples

Example 1

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

Explanation: The values in range are 7, 10, and 15, so the sum is 32.

Example 2

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

Explanation: The values in range are 6, 7, and 10, so the sum is 23.

Constraints

  • The number of nodes is in the range [0, 2 * 10^4]
  • 1 <= Node.val <= 10^5
  • 1 <= low <= high <= 10^5
  • The input tree satisfies BST ordering rules

A correct solution should use the BST property to avoid unnecessary traversal when possible.

Examples

Example 1
Inputroot = [10,5,15,3,7,null,18], low = 7, high = 15Output32WhyThe node values within the inclusive range are 7, 10, and 15.
Example 2
Inputroot = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10Output23WhyThe valid node values are 6, 7, and 10, so the sum is 23.

Constraints

  • The number of nodes is in the range [0, 2 * 10^4]
  • 1 <= Node.val <= 10^5
  • 1 <= low <= high <= 10^5
  • The input tree is a valid binary search tree

Function Signature

def range_sum_bst(root, low, high):
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
MetaRange Sum in Binary TreeMediumMetaVertical Column Sums in Binary TreeMediumMetaVertical Column Sums in Binary TreeMedium
Next question