← 返回 citadel 的题目列表Minimum Path Sum to Target in Binary Tree (Variant of LC 112)
类型:online_judge
Problem: Minimum Path to Target in a Binary Tree (Variant of LC 112)
Given the root of a binary tree root and an integer targetSum. A root-to-leaf path is a sequence of nodes starting at the root and going down following parent-child pointers.
Among all root-to-leaf paths whose node values sum to targetSum, return the minimum path as a list of node values.
If multiple valid paths exist (their sums are all targetSum), break ties by returning the one with the fewest nodes.
If still tied, return the path whose node-value sequence is lexicographically smallest.
If no such path exists, return an empty list [].
Input Format
Line 1: level-order array representation of the tree tree (use null for missing nodes).
Line 2: integer targetSum.
Output Format
Print an array representing the selected path; print [] if none.
Constraints
Number of nodes n: 0 <= n <= 2 * 10^5
Node values: -10^9 <= val <= 10^9
targetSum: -10^14 <= targetSum <= 10^14
Examples
Example 1
Input:
[5,4,8,11,null,13,4,7,2,null,null,5,1]
22
Output:
[5,4,11,2]
Example 2
Input:
[1,2,3]
5
Output:
[]
Example
Input
[5,4,8,11,null,13,4,7,2,null,null,5,1]
22
Output
[5, 4, 11, 2]