Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Range Sum in Binary Tree

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Range Sum in Binary Tree. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

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]

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output