# LeetCode: Binary Trees With Factors Solution

## Approach

Multiplying as result of combination

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**2 * @param {number[]} arr3 * @return {number}4 */5var numFactoredBinaryTrees = function (arr) {6  const set = new Set(arr)7  const memo = new Map()8  const recursion = el => {9    if (memo.has(el)) {10      return memo.get(el)11    }12    let count = 113    for (const otherEl of arr) {14      if (el % otherEl === 0 && set.has(el / otherEl)) {15        count += recursion(otherEl) * recursion(el / otherEl)16      }17    }18    memo.set(el, count)19    return count20  }21
22  return arr.reduce((acc, el) => (acc += recursion(el)), 0) % (1e9 + 7)23}```

## Tags

leetcode

recursion

dynamic programming

## Next Post

LeetCode: Find Center Of Star Graph

Mar 14, 2021

Search Posts