Does Every Node Equal the Sum of Its Children

Implement childrenAddUp

You are given the root of a binary tree of integers. The tree has the children-sum property if every node that has at least one child holds a value equal to the sum of the values of its children, where a missing child counts as 0. Leaves are not checked. Return true if the property holds at every node, and false otherwise. An empty tree satisfies the property.

A level-order sweep with a queue checks every node in turn. A recursion checks the same thing while stopping at the first violation and using only the call stack.

Example 1:

Input: root = [40,15,25,10,5,20,5]

Output: true

Example 2:

Input: root = [40,15,25,10,5,20,6]

Output: false

Example 3:

Input: root = [9,9]

Output: true

+ 13 hidden test cases run on Submit.

Constraints:

  • ●0 ≤ number of nodes ≤ 100
  • ●−100 ≤ node.val ≤ 100
  • ●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
  • ●The tree has the children-sum property when every node that has at least one child holds a value equal to the sum of its children's values (a missing child counts as 0). Leaves are never checked. Return true if the property holds at every node, false otherwise; an empty tree qualifies

root =

[40, 15, 25, 10, 5, 20, 5]