[LeetCode]题解(python):096-Unique Binary Search Trees

题目来源:

  https://leetcode.com/problems/unique-binary-search-trees/



题意分析:

  给定一个整数n,返回所有中序遍历是1到n的树的可能。



题目思路:

  这是动态规划的题目。选定了第k个为根节点,那么所有的可能就是ans[k-1] * ans[n -k]其中,ans[i]代表i整数i一共有ans[i]种可能。



代码(python):

  

class Solution(object):
    def numTrees(self, n):
        """
        :type n: int
        :rtype: int
        """
        ans = [0 for i in range(n + 1)]
        ans[0],ans[1] = 1,1
        for i in range(2,n+1):
            for j in range(i/2):
                ans[i] += ans[j]*ans[i - 1 - j]
            ans[i] *= 2
            if i % 2 == 1:
                ans[i] += ans[i/2]*ans[i/2]
        return ans[n]

时间: 2024-10-14 15:02:19

[LeetCode]题解(python):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)))

LeetCode之“动态规划”:Unique Binary Search Trees && 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 题目分析参考自一博

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

【Leetcode 96】96. Unique Binary Search Trees

This is a BST(binary search tree) numbers counting problem but solved in dynamic programing. https://discuss.leetcode.com/category/104/unique-binary-search-trees This solution really gives me a shock. Before solving u need do some mathematical deduce

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

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 @ 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 #

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,#

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