Example 1
Input
root = TreeNode(5, TreeNode(4, TreeNode(11, TreeNode(7), TreeNode(2))), TreeNode(8, TreeNode(13), TreeNode(4, None, TreeNode(1)))); target_sum = 22
Output
True
One path 5 -> 4 -> 11 -> 2 sums to 22.
Full lesson preview
Determine whether a binary tree has any root-to-leaf path whose node values sum to a target.
Problem statement
Task
Examples
Input
root = TreeNode(5, TreeNode(4, TreeNode(11, TreeNode(7), TreeNode(2))), TreeNode(8, TreeNode(13), TreeNode(4, None, TreeNode(1)))); target_sum = 22
Output
True
One path 5 -> 4 -> 11 -> 2 sums to 22.
Input format
Output format
Constraints
Samples
Input
root = TreeNode(1, TreeNode(2)); target_sum = 3
Output
True
Path 1 -> 2 sums to 3.