unique-binary-search-trees Java code
2017-11-01 13:23
639 查看
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
/ / \ \
2 1 2 3
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
/ / \ \
2 1 2 3
public class Solution { public int numTrees(int n) { if(n<0){ return -1; } int[] dp=new int[n+1]; dp[0]=1; dp[1]=1; for(int i=2;i<=n;i++){ for(int j=0;j<i;j++){ dp[i]+=dp[j]*dp[i-j-1]; } } return dp ; } }
相关文章推荐
- unique-binary-search-trees Java code
- CODE 37: Unique Binary Search Trees II
- Java for LeetCode 096 Unique Binary Search Trees
- [leet code] Unique Binary Search Trees II
- LeetCode96_Unique Binary Search Trees(求1到n这些节点可以组成多少种不同的二叉查找树) Java题解
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
- [leet code] Unique Binary Search Trees
- leetcode JAVA Unique Binary Search Trees 难度系数3 3.34
- LeetCode 96 — Unique Binary Search Trees(C++ Java Python)
- lintcode: Unique Binary Search Trees
- (java)Unique Binary Search Trees
- Java [Leetcode 96]Unique Binary Search Trees
- leecode 解题总结:96. Unique Binary Search Trees
- [Leetcode] Unique Binary Search Trees II (Java)
- [LeetCode][Java]Unique Binary Search Trees II
- LeetCode96_Unique Binary Search Trees(求1到n这些节点能够组成多少种不同的二叉查找树) Java题解
- Lettcode_96_Unique Binary Search Trees
- Unique Binary Search Trees (Java)
- leetcode-java-95. Unique Binary Search Trees II