A straightforward solution using Catalan Number, O(1) space, O(1) time, 2 lines


  • -4
    A
    class Solution {
    public:
        int numTrees(int n) {
            int a[] = {1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190};
            return a[n];
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.