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

给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?
例如,
给出 n = 3,则有 5 种不同形态的二叉查找树:
   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3
详见:https://leetcode.com/problems/unique-binary-search-trees/description/

class Solution {
public:
    int numTrees(int n) {
        vector<int> dp(n+1,0);
        dp[0]=1;
        dp[1]=1;
        for(int i=2;i<=n;++i)
        {
            for(int j=0;j<i;++j)
            {
                dp[i]+=dp[j]*dp[i-j-1];
            }
        }
        return dp[n];
    }
};

参考:https://www.cnblogs.com/grandyang/p/4299608.html

原文地址:https://www.cnblogs.com/xidian2014/p/8717545.html

时间: 2024-11-05 15:45:06

096 Unique Binary Search Trees 不同的二叉查找树的相关文章

096 Unique Binary Search Trees

题目: 096 Unique Binary Search Trees 这道题目就是catalan数, 因为递归关系 为       代码为 class Solution: # @param {integer} n # @return {integer} def numTrees(self, n): # Catalan Number ans = 1 for i in range(1, n+1): ans *= (n+i)*1.0/i return int(round(ans/(n+1)))

Java for LeetCode 096 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. 解题思路: 二叉查找树,刚上来无从下手,但仔细想想就能发现规律:以i为根节点,i的左子树都是小于i的,共有numTrees(i-1)种情况,i的右子树都是大于i的,共有numTrees(n-i

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: Unique Binary Search Trees II [096]

[题目] 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,#

【LeetCode】Unique Binary Search Trees II 不同的二叉查找树II

今早起来做 LeetCode,结果被这道题卡了将近1个半小时,忍着没有去搜答案,最后还是被我想出来了,而且直接一次AC,哈哈!现在在这里记录一下解题思路. 原题: 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 sh

Leetcode 树 Unique Binary Search Trees

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Binary Search Trees Total Accepted: 13478 Total Submissions: 37858 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there

[leetcode]Unique Binary Search Trees II @ Python

原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上一题要求的是符合条件的二叉查找树的棵数,我们上一题提过,求个数一般思路是动态规划,而枚举的话,我们就考虑dfs了.dfs(start, end)函数返回以start,start+1,...,end为根的二叉查找树. 代码: # Definition for a binary tree node #

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