#C12056. Binary Tree Path Sum
Binary Tree Path Sum
Binary Tree Path Sum
Given a binary tree represented in level-order (breadth-first traversal), determine whether there exists a path from the root to a leaf such that the sum of the node values equals a given target sum. Formally, you need to decide if there exists a path satisfying $$\sum_{node\in path}{node.val} = target\_sum$$.
The binary tree may contain missing nodes which are indicated by the keyword null
.
inputFormat
The input is read from standard input and consists of three parts:
- The first line contains an integer
n
, representing the number of tokens (node values) provided in level-order. - The second line contains
n
tokens separated by spaces. Each token is either an integer or the stringnull
, which represents a missing node. - The third line contains an integer representing the target sum.
outputFormat
Output a single line to standard output: YES
if there exists a root-to-leaf path whose node values sum to the target sum, otherwise output NO
.
5
5 4 8 11 13
22
YES
</p>