Recursive + Memo || C++ & Java (Detailed explanation))
C++\nRecursive Solution\n\n\nclass Solution {\n    int mod = 1e9 + 7;\n    unordered_set<int> st;\nprivate:\n    int factorCount(int root)\n    {\n        // co
**C++**\n**Recursive Solution**\n\n```\nclass Solution {\n    int mod = 1e9 + 7;\n    unordered_set<int> st;\nprivate:\n    int factorCount(int root)\n    {\n        // count = 1 , root element will also be our one of the ans\n        int count = 1;\n        \n        // iterate through set\n        for(auto &val : st)\n        {\n            // check if root is divisible or not\n            if(root % val != 0) continue;\n            \n            // if its divisible, call recursively for its factor\n            int factor1 = val, factor2 = root / val;\n            \n            // check its factor is present in given arr\n            if(st.count(factor2))\n            {\n                count += factorCount(factor1) * factorCount(factor2);\n                count %= mod;\n            }\n            \n         }\n        \n        return count;\n    }\n    \npublic:\n    int numFactoredBinaryTrees(vector<int>& arr) {\n        \n        sort(arr.begin(), arr.end());\n        \n        st.insert(arr.begin(), arr.end());\n        \n        int res = 0;\n        \n        for(auto &curVal : arr)\n        {\n            // check from root to leaf\n            // make cur val as root and find its factors i.e leaves\n            res += factorCount(curVal);\n        }\n        \n        return res;\n    }\n};\n```\n\n**Recursion + Memoization**\n\n```\nclass Solution {\n    int mod = 1e9 + 7;\n    unordered_map<int, long long int> dp;\n    unordered_set<int> st;\nprivate:\n    long long int factorCount(int root)\n    {\n        // check in dp, if count has been calculated already for this root\n        if(dp.count(root))\n            return dp[root];\n        \n        // count = 1 , root element will also be our one of the ans\n        long long int count = 1;\n        \n        // iterate through set\n        for(auto &val : st)\n        {\n            // check if root is divisible or not\n            if(root % val != 0) continue;\n            \n            // if its divisible, call recursively for its factor\n            int factor1 = val, factor2 = root / val;\n            \n            // check its factor is present in given arr\n            if(st.count(factor2))\n            {\n                count += factorCount(factor1) * factorCount(factor2);\n                count %= mod;\n            }\n            \n         }\n        \n        return dp[root] = count;\n    }\n    \npublic:\n    int numFactoredBinaryTrees(vector<int>& arr) {\n                \n        st.insert(arr.begin(), arr.end());\n        \n        int res = 0;\n        \n        for(auto &curVal : arr)\n        {\n            // check from root to leaf\n            // make cur val as root and find its factors i.e leaves\n            res += factorCount(curVal);\n            res %= mod;\n        }\n        \n        return res;\n    }\n};\n```\n\n**Java (Recursion + Memo)**\n```\nclass Solution {\n    private int mod = (int) 1e9 + 7;\n    Set<Integer> st = new HashSet<>();\n    Map<Integer, Long> dp = new HashMap<>();\n    \n    \n    private long factorCount(int root)\n    {\n        if(dp.containsKey(root))\n            return dp.get(root);\n        \n        // considering root as well into count, so only count = 1\n        long count = 1;\n        \n        for(int val : st)\n        {\n            // check if its divisible or not\n            if(root % val != 0) continue;\n            \n            int factor1 = val, factor2 = root/val;\n            \n            // check factors are in set\n            if(st.contains(factor2))\n            {\n                // call recursively for its factors\n                count += factorCount(val) * factorCount(factor2);\n                count %= mod;\n            }\n        }\n        \n        // insert into map\n        dp.put(root, count);\n        \n        return count;\n    }\n    \n    public int numFactoredBinaryTrees(int[] arr) {\n        \n        // add elements in set\n        for(int val : arr)\n            st.add(val);\n        \n        int res = 0;\n        \n        // iterate each element in arr, consider as root\n        for(int curVal : arr)\n        {\n            res += factorCount(curVal);\n            res %= mod;\n        }\n        \n        return res;\n    }\n}\n```
['Recursion', 'Memoization', 'C', 'Java']