【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】

【096-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

题目大意

  给定一个n个结点的二叉搜索树,求一共有多少个不同类型的二叉搜索树。

解题思路

  递推公式

  f(k)*f(n-1-k):f(k)表示根结点左子树有k个结点,其有的形状是f(k),f(n-1-k)表示右子树有n-1-k个结点

  f(n) = 2*f(n-1) + f(1)*f(n-2) + f(2)*f(n-3) + f(3)*f(n-4) + … +f(n-2)*f(1)

代码实现

算法实现类

public class Solution {

    public int numTrees(int n) {

        if (n <= 0) {
            return 0;
        } else if (n == 1) {
            return 1;
        }

        int[] result = new int[n + 1];
        result[0] = 0;
        result[1] = 1;

        // 求f(2)...f(n)
        for (int i = 2; i <= n; i++) {
            // 求f(i)
            result[i] = 2 * result[i - 1];
            for (int j = 1; j <= i - 1 ; j++) {
                result[i] += result[j]*result[i - 1 -j];
            }

        }
        return result[n];
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47333321

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-25 08:10:53

【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】的相关文章

leetCode 96.Unique Binary Search Trees (唯一二叉搜索树) 解题思路和方法

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] 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 这道题实际上是Catalan Number卡塔兰数的一个例子,如果对卡塔兰数不熟悉的童鞋可能真不太好做

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] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the sequence is unique. Follow up:Could you do it using only constant space complexity? 给一个数组,验证是否为一个二叉搜索树的

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] Recover Binary Search Tree 复原二叉搜索树

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#,2,3}"

[LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Example 1: Input: [1,3,null,null,2]   1   /  3     2 Output: [3,1,null,null,2]   3   /  1     2 Example 2: Input: [3,1,4,null,null,2]

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] 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 node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

[LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses