// Source : https://oj.leetcode.com/problems/unique-binary-search-trees/ // Author : Hao Chen // Date : 2014-06-25 /********************************************************************************** * * Given n, how many structurally unique BST's (binary search trees) that store values 1...n? * * For example, * Given n = 3, there are a total of 5 unique BST's. * * 1 3 3 2 1 * \ / / / \ \ * 3 2 1 1 3 2 * * **********************************************************************************/ #include #include #include int numTrees1(int n) ; int numTrees2(int n) ; int numTrees(int n) { return numTrees1(n); } int numTrees1(int n) { int *cnt = (int*)malloc((n+1)*sizeof(int)); memset(cnt, 0, (n+1)*sizeof(int)); cnt[0] = 1; cnt[1] = 1; for (int i=2; i<=n; i++){ for(int j=0; j1){ n = atoi(argv[1]); } printf("%d=%d\n", n, numTrees(n)); return 0; }