LeetCode: Unique Binary Search Trees [095]

【题目】

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,4,5...n这n个值,能构造多少棵合法的二叉搜索树

【思路】

对于给定[1,n]区间,先确定所有可能的根,假设根为k, 则该二叉树左子树的取值区间为[1, k-1]和右子树的取值区间[k+1, n]。

而二叉搜索搜索树的任意一个节点的左右子树也是二叉搜索树,因此我们需要确定[1,k-1]和[k+1, n]上构造的二叉搜索树的数目, 比如分别为left[k], right[k]。则以k的根的二叉搜索树的数目即为,left[k]*right[k]

本题用递归来解决。

【代码】

class Solution {
public:

    int binaryTreeNums(int start, int end){
        //[start, end]区间上构造二叉树的数目
        if(start>=end)return 1;     //start<end表示空子树, start==end表示叶子节点

        int treeNums=0;

        for(int root=start; root<=end; root++){
            int leftCount = binaryTreeNums(start, root-1);  //计算左子树的数目
            int rightCount = binaryTreeNums(root+1, end);   //计算右子树的数目
            treeNums+= leftCount*rightCount;
        }

        return treeNums;
    }

    int numTrees(int n) {
        if(n==0)return 0;
        return binaryTreeNums(1, n);
    }
};

LeetCode: Unique Binary Search Trees [095]

时间: 2024-12-28 00:22:51

LeetCode: Unique Binary Search Trees [095]的相关文章

[LeetCode] 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 class Solution { private

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

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 原题链接:https://oj.leetcode.com/problems/unique-binar

LeetCode:: Unique Binary Search Trees[详细分析]

Unique Binary Search Trees My Submissions 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 设S(n)为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 \      

[LeetCode] 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——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 原题链接:https://oj.leetcode

LeetCode 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 1 /** 2 * Definition for