Problem · Tree
Minimum Path Sum to Target in Binary Tree
Learn this problemProblem statement
You are given a binary tree and an integer targetSum. A valid path is a root-to-leaf path whose node values add up to targetSum.
Among all valid paths, return the minimum one using the following tie-breaking rules:
- Prefer the path with fewer nodes.
- If lengths are equal, prefer the lexicographically smaller node-value sequence.
If no valid path exists, return an empty array.
Function
findMinimumTargetPath(levelOrder: String[], targetSum: int) → int[]Complete the function findMinimumTargetPath in the editor below.
findMinimumTargetPath has the following parameters:
String[] levelOrder: the binary tree in level-order form, using"null"for missing childrenint targetSum: the required path sum
Returns
int[]: the chosen root-to-leaf path, or an empty array if none exists.
Examples
Example 1
levelOrder = ["5", "4", "8", "11", "null", "13", "4", "7", "2", "null", "null", "5", "1"]targetSum = 22return = [5, 4, 11, 2]The path 5 -> 4 -> 11 -> 2 sums to 22 and is the selected valid path.
Example 2
levelOrder = ["1", "2", "3"]targetSum = 5return = []No root-to-leaf path sums to 5, so the answer is empty.
Constraints
- The input tree is finite and represented in level order.
- The returned path must start at the root and end at a leaf.
- If multiple valid paths exist, apply the required tie-breaking rules exactly.