Does Any Root-to-Leaf Route Add Up to the Target

Implement routeAddsUp

You are given the root of a binary tree and an integer target. A route starts at the root, goes downward through children, and must end at a leaf (a node with no children). Return true if some route has node values that add up to exactly target, and false otherwise. An empty tree has no routes.

You can list the total of every route and check the list, or you can subtract each node's value from the target as you go down, so a leaf only needs to compare its own value with what remains.

Example 1:

Input: root = [6,3,8,1,4,null,9,null,2], target = 13

Output: true

Example 2:

Input: root = [6,3,8,1,4,null,9,null,2], target = 10

Output: false

Example 3:

Input: root = [], target = 0

Output: false

+ 14 hidden test cases run on Submit.

Constraints:

  • ●0 ≤ number of nodes ≤ 100
  • ●−100 ≤ node.val ≤ 100; −1000 ≤ target ≤ 1000
  • ●The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child
  • ●A route starts at the root, moves downward and must END at a leaf (a node with no children). Return true if the values along at least one such route add up to exactly target; an empty tree has no route

root =

[6, 3, 8, 1, 4, null, 9, null, 2]

target =

13