#K11871. Path Sum in Binary Tree

    ID: 23565 Type: Default 1000ms 256MiB

Path Sum in Binary Tree

Path Sum in Binary Tree

You are given a binary tree and an integer targetSum. Your task is to determine the number of paths in the tree such that the sum of the node values along that path equals targetSum. A valid path is defined as any sequence of nodes starting from some node and moving only downward (i.e. from parent to child). The path does not need to start at the root or end at a leaf.

The binary tree is provided in level order as space‐separated tokens. Use the word null (case sensitive) to denote a missing node.

Note: There will be only one test case per execution. You should read input from standard input and write output to standard output.

inputFormat

The input consists of two lines:

  • The first line contains space-separated tokens representing the binary tree in level order. Use null to indicate a missing node.
  • The second line contains an integer representing targetSum.

outputFormat

Output a single integer: the number of valid paths in the binary tree whose node values sum up to targetSum. Print the result to standard output.

## sample
10 5 -3 3 2 null 11 3 -2 null 1
8
3