Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1…n.
For example,
Given n = 3, your program should return all 5 unique BST’s shown below.
解法1: Recursive, Divide & Conquer
用分治的思想考虑会比较容易,从1到n,如果选择了i,那么1到i-1所能组成的tree都是i的左子树,i+1到n都为右子树。对于每一颗左子树和右子树的组合,我们都能构建一个新的树。
用递归的方式很容易解决。
C++
Java