Unique Binary Search Trees

Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n?

Example:

Input: 3
Output: 5
Explanation:
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

BST树的特点是比根节点小的数都在左子树,比根节点大的数都在右子树.
1-i 的bst树总共有多少种结构.记做g(i);
等于1-i轮流作为根节点的总和.
当t为根节点,左边有t-1个数,右边有i-t个数,g(t-1)+g(i-t);

posted @ 2019-04-17 20:47  少年留不住  阅读(79)  评论(0编辑  收藏  举报