【leetcode】23.Unique Binary Search Trees

以i为根节点时,其左子树构成为[0,...,i-1],其右子树构成为[i+1,...,n]构成。根结点确定时,左子树与右子树的结点个数都是确定的。

这样就可以把这个问题化成子问题。因此可以用动态规划解。

Sigma(左边的子树可能状态 * 右边子树可能状态) = 当前个数的结点可能的状态数。

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

public class Solution {

    public
int numTrees(int
n) {

        int
nums[] = new
int[n+1];

        if(n<=1)

            return
1;

        nums[0]=1;

        nums[1]=1;

        for(int
i=2; i<=n; i++){

            nums[i]=0;

            for(int
j=0; j<i; j++){

                int
leftNum = nums[j];

                int
rightNum = nums[i-j-1];

                nums[i] += leftNum * rightNum;

                

            }

        }

        return
nums[n];

    }

}

  JAVA效率就是比较低啊……300+ms。 同样的过程用C写就4ms。

时间: 2024-10-09 14:15:50

【leetcode】23.Unique Binary Search Trees的相关文章

【LeetCode】96 - 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 Tree Dynamic Programming Solution 1:  递归 1 class So

【一天一道LeetCode】#96. Unique Binary Search Trees

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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. (二)解题

【LeetCode】96. Unique Binary Search Trees-唯一二叉排序树的个数

一.描述: 二.思路: BST(二叉排序树):中序遍历的结果为非递减序列,并且节点(个数和值)相同的不同二叉树的中序遍历结果都相同: 当左子树的节点个数确定后,右子树的个数也随之确定: 当节点个数为0或1时,二叉树只有1种,表示为f(0)=1,f(1)=f(0)*f(0): 当节点个数为2时,总的种类数=左子树为空f(0)*右子树不为空f(1)+左子树不为空f(1)*右子树为空f(0),即f(0)*f(1)+f(1)*f(0)=2种: 当节点个数为3时,有左子树为空f(0)*右子树不为空f(2)

leetcode || 95、Unique Binary Search Trees II

problem: 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 confused what "

【LeetCode】98. Validate Binary Search Tree

Difficulty:Medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/validate-binary-search-tree/ Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a nod

LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

题目如下所示:返回的结果是一个Node的Vector: 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

LeetCode OJ 95. 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 confused what "{1,#,2,3}&

leetcode || 96、Unique Binary Search Trees

problem: 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 Hide Tags Tree Dynamic Programming 题意:大小为

LeetCode OJ: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 一开始是想用递归解决,但看了标签是dp问题,就想了一下, 数目为k的bst,其每个 0 ~ k - 1