Determine if tree has path sum
Company: Chicago
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
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.
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
- DFS with a running sum.