Unique Binary Search Trees

来源:互联网 发布:java web开发pdf 中文 编辑:程序博客网 时间:2024/06/06 19:06

问题描述:

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
解决思路:

首先分析一下当n=0,1,2这三种情况下BTS个数,我们可以得到当n=0,1时,BTS个数为1,当n=2时,BTS个数为2.这就可以转化为一个递归求解的问题,我们将根结点从1到n依次代替,这样左右子树结点的个数就会发生变化,我们在分别求解左右子树BTS的个数,以此递归求解便可得到BTS的总数目。

C++

class Solution {public:    int numTrees(int n) {        if(n == 1 || n == 0)            return 1;        int num = 0;        for(int i = 1; i <= n; i++)            num += numTrees(i-1) * numTrees(n-i);        return num;    }};


0 0
原创粉丝点击