PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of binary tree data structures and traversal techniques, along with the ability to accumulate and track root-to-leaf path sums while correctly handling edge cases such as empty trees and negative values.

  • easy
  • Chicago
  • Coding & Algorithms
  • Data Scientist

Determine if tree has path sum

Company: Chicago

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

## Problem Given the `root` of a binary tree and an integer `targetSum`, return `true` if the tree has a **root-to-leaf** path such that adding up all the values along the path equals `targetSum`. Otherwise, return `false`. - A **leaf** is a node with no children. - A **root-to-leaf** path starts at the root and ends at any leaf. ## Input - `root`: binary tree node with fields `val`, `left`, `right` - `targetSum`: integer ## Output - Boolean indicating whether such a path exists. ## Example For a tree with root-to-leaf path values `[5,4,11,2]` and `targetSum = 22`, return `true` because `5 + 4 + 11 + 2 = 22`. ## Notes / Constraints (assumptions) - Node values and `targetSum` may be negative. - You should handle an empty tree (`root = null`). ## Task Implement a function (e.g., in Python) to solve this problem efficiently.

Quick Answer: This question evaluates understanding of binary tree data structures and traversal techniques, along with the ability to accumulate and track root-to-leaf path sums while correctly handling edge cases such as empty trees and negative values.

Return True if any root-to-leaf path sums to targetSum. The tree may be a dict with val/left/right or a [val,left,right] list.

Constraints

  • Node values may be negative
  • Empty tree returns False

Examples

Input: ({'val': 5, 'left': {'val': 4, 'left': {'val': 11, 'left': {'val': 7}, 'right': {'val': 2}}}, 'right': {'val': 8, 'left': {'val': 13}, 'right': {'val': 4, 'right': {'val': 1}}}}, 22)

Expected Output: True

Input: ({'val': 5, 'left': {'val': 4, 'left': {'val': 11, 'left': {'val': 7}, 'right': {'val': 2}}}, 'right': {'val': 8, 'left': {'val': 13}, 'right': {'val': 4, 'right': {'val': 1}}}}, 26)

Expected Output: True

Input: (None, 0)

Expected Output: False

Input: ([-2, None, [-3]], -5)

Expected Output: True

Hints

  1. DFS with a running sum.
Last updated: Jun 27, 2026

Related Coding Questions

  • Merge Nested Dictionaries Iteratively - Chicago (easy)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.