[Java]LeetCode96 Unique Binary Search Trees

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值,那么从1.2.3....N共N个数,能构建几棵二叉排序树。

解题思路就是:遍历1至N个数,求它们每个元素作为根节点的情况,根据中序遍历的思想,小于根节点的排在左边,大于根节点的排在右边。求出左边子二叉树的构建数目和右二叉树的构建数目,相乘就得到该二叉树的数目了。该题是不是感觉又可以根据递归的思路来解了。

递归思路:

 public int numTrees(int n) {
       if(n==0||n==1)return 1;
       if(n==2)return 2;
       int num=0;
       for(int i=1;i<=n;i++)//根节点
       {
           num+=numTrees(i-1)*numTrees(n-i);//左子树二叉排序树的构建数目*右子树二叉排序树的构建数目
       }
       return num;
    }

虽然逻辑是对的,但是leetcode提示timeLimited。也就是递归超时了。

那我们试一试将已经作为根节点求解的构建数目先用数组保存起来,然后后面的节点求解的时候再调用。

1)先定义一个array

2)array[0]表示节点数为0的情况,array[0]=1;

3)array[1]表示1个节点的求解只有一种情况:左节点的个数和右节点个数都为0即a[0]*a[0];

4)array[2]表示2个节点的求解只有两种情况:左节点的个数和右节点个数都为a[1]*a[0],a[0]*a[1]

5)array[3]表示3个节点的求解只有三种情况:左节点的个数和右节点个数为a[2]*a[0],a[1]*a[1],a[0]*a[2]

以此类推

我们用代码实现如下

public int numTrees(int n) {
       int[] array=new int[n+1];
       array[0]=1;
       for(int i=1;i<=n;i++)
       {
         for(int j=0;j<i;j++)
         {
          array[i]+=array[j]*array[i-j-1];
         }
       }
       return array[n];
    }
时间: 2024-10-17 06:15:46

[Java]LeetCode96 Unique Binary Search Trees的相关文章

LeetCode96:Unique Binary Search Trees

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,求1到n这些数能够构成多少棵二叉树. 给定一个序列1.....n,为了构造全部二叉树.

Unique Binary Search Trees II leetcode java

题目: Given 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 题解:这道题比1难的就是不是返回个数,而

Unique Binary Search Trees leetcode java

题目: 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 题解:  这道题我自己是没啥思路的,感觉就是一种排列组合的计算,但是又不太会..然后网上查了

Java for LeetCode 095 Unique Binary Search Trees II

Given 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. 解题思路: 参考Java for LeetCode 096 Unique Binary Search Trees思路,本题很容易解决.注意,

[LeetCode][Java]Unique Binary Search Trees II

题目: Given 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 题意: 给定n ,生成所有的存在且唯一的

LeetCode: Unique Binary Search Trees II 解题报告

Unique Binary Search Trees II Given 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 \      

Unique Binary Search Trees I &amp; II

Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? 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 分析: 当数组为 1,2,3,4,.. i,.. n时,基于以下原则的BST建树具有唯一性:以i为根节点的树,其

[LeetCode][JavaScript]Unique Binary Search Trees II

Unique Binary Search Trees II Given 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

LeetCode之“动态规划”:Unique Binary Search Trees &amp;&amp; Unique Binary Search Trees II

1. Unique Binary Search Trees 题目链接 题目要求: 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 题目分析参考自一博