Unique Binary Search Trees(不同的二叉查找树)

问题

Given n, how many structurally unique BSTs (binary search trees) that store values 1...n?

Have you met this question in a real interview? Yes
Example
Given n = 3, there are a total of 5 unique BST's.

Unique Binary Search Trees(不同的二叉查找树)_第1张图片

代码

参阅 卡特兰数

分析

public class Solution {
    /**
     * @paramn n: An integer
     * @return: An integer
     */
    public int numTrees(int n) {
        // write your code here
        int[] res=new int[n+2];
        res[0]=1;
        res[1]=1;
        for(int i=2;i<=n;i++){
            for(int j=0;j

你可能感兴趣的:(Unique Binary Search Trees(不同的二叉查找树))