Java Simple Soltuion


  • 0
    M
    public class Solution {
        public int numTrees(int n) {
            int t[] = new int[n+1];
            t[0] = 1;
            for (int i=1; i<=n; ++i)
                for (int j=1; j<=i; ++j)
                    t[i] += t[j-1]*t[i-j];
            return t[n];
        }
    }
    

    You can find the same idea with explanations here. The key is to choose a value as root and see what you can put on both sides.


Log in to reply
 

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