Unique Binary Search Trees II

update Aug 13, 2017 15:26 update Dec 23, 2017 0:26

LeetCodearrow-up-right

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         3     3      2      1
  \       /     /      / \      \
   3     2     1      1   3      2
  /     /       \                 \
 2     1         2                 3

Basic Idea:

首先明确一点,很多时候求解的数量时用dp,但是求所有解的时候一定要用dfs。(用dp求此题解的数量的题目笔记在 这里arrow-up-right)

  所以这里首先考虑dfs的解法。我们可以定义一个list<TreeNode> helper(int start, int end)函数,这个函数的定义是:返回一个list,包含[start, end]这个序列组成的所有BST。我们利用这个函数,对于一个输入 n 即 [1,n],我们可以令其内每个元素为root,然后生成其左右两子序列的所有BST,然后只要以其本身为root,将生成的左右子树们两两配对组装即可。

时间复杂度分析:

空间复杂度分析:

Python Code:

Java Code:

update 2018-06-03 11:43:40

C++ Code: