FastPrepFastPrep
Problem Brief

Minimum Path Sum to Target in Binary Tree

FULLTIMEPHONE SCREEN

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 Description

Complete the function findMinimumTargetPath in the editor below.

findMinimumTargetPath has the following parameters:

  1. String[] levelOrder: the binary tree in level-order form, using "null" for missing children
  2. int targetSum: the required path sum

Returns

int[]: the chosen root-to-leaf path, or an empty array if none exists.

1Example 1

Input
levelOrder = ["5", "4", "8", "11", "null", "13", "4", "7", "2", "null", "null", "5", "1"], targetSum = 22
Output
[5, 4, 11, 2]
Explanation

The path 5 -> 4 -> 11 -> 2 sums to 22 and is the selected valid path.

2Example 2

Input
levelOrder = ["1", "2", "3"], targetSum = 5
Output
[]
Explanation

No root-to-leaf path sums to 5, so the answer is empty.

Constraints

Limits and guarantees your solution can rely on.

  • 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.
public int[] findMinimumTargetPath(String[] levelOrder, int targetSum) {
    // write your code here
}
Input

levelOrder

["5", "4", "8", "11", "null", "13", "4", "7", "2", "null", "null", "5", "1"]

targetSum

22

Output

[5, 4, 11, 2]

Sign in to submit your solution.