← 返回 oracle 的题目列表Find all root-to-leaf paths with target sum in binary tree
类型:online_judge
oracle
Suppose you have a binary tree, please write a function to accomplish the following:
Find all paths in the binary tree where the sum of the node values equals a given integer targetSum.
Provide a function signature as def find_paths(root: Optional[TreeNode], targetSum: int) -> List[List[int]]:.
Input Description:
root is the root node of a binary tree of type Optional[TreeNode].
targetSum is an integer.
Output Description:
Return a list containing all the paths that satisfy the condition, with each path represented as a list of node values.
Example:
Input:
root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output:
[[5,4,11,2],[5,8,4,5]]
Constraints:
The number of nodes is in the range [0, 5000].
Each node's value is in the range [-1000, 1000].
Example
Input
root = [5,4,8,11,null,13,4,7,2,null,null,5,1]
targetSum = 22