Binary Tree Root to Leaf Path Sum - aloalgo

Binary Tree Root to Leaf Path Sum

Medium

You are given the root of a binary tree and an integer target_sum.\n\nYour task is to determine if there exists a root-to-leaf path in the binary tree such that the sum of all node values along that path equals target_sum.\n\nNote that:\n* A leaf is defined as a node with no children.\n* A root-to-leaf path is a sequence of nodes starting from the root node and ending at any leaf node in the tree.\n\nReturn True if such a path exists, otherwise return False.

Example 1

Inputs
54117281341
target_sum = 22
Output
True
Explanation:

The path 5 -> 4 -> 11 -> 2 has a sum of 5 + 4 + 11 + 2 = 22.

Example 2

Inputs
123
target_sum = 5
Output
False
Explanation:

No root-to-leaf path sums to 5. The paths are 1 -> 2 (sum 3) and 1 -> 3 (sum 4).

Example 3

Inputs
1
target_sum = 1
Output
True
Explanation:

The only root-to-leaf path is 1, which sums to 1.

Loading...
Inputs
54117281341
target_sum = 22
Output
True

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!