LeetCode: Unique Binary Search Trees I & II

Title:

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

首先注意这里是BST而不是普通的Binary Tree,所以数字会对插入的位置有影响。这类找combination/permutation的题都需要找找规律。

n = 0

n = 1

1

n = 2

1                  2

\                /

2            1

n = 3

1           3    3      2     1

\        /     /       / \       \

3    2    1      1   3      2

/     /        \                    \

2   1          2                   3

定义f(n)为unique BST的数量,以n = 3为例:

构造的BST的根节点可以取{1, 2, 3}中的任一数字。

如以1为节点,则left subtree只能有0个节点,而right subtree有2, 3两个节点。所以left/right subtree一共的combination数量为:f(0) * f(2) = 2

以2为节点,则left subtree只能为1,right subtree只能为2:f(1) * f(1) = 1

以3为节点,则left subtree有1, 2两个节点,right subtree有0个节点:f(2)*f(0) = 2

总结规律:

f(0) = 1

f(n) = f(0)*f(n-1) + f(1)*f(n-2) + ... + f(n-2)*f(1) + f(n-1)*f(0)

使用动态规划的代码

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

Title:

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}" means? > read more on how binary tree is serialized on OJ.

要求生成所有的unique BST,类似combination/permutation的题目,可以递归构造。

1. 根节点可以任取min ~ max (例如min  = 1, max = n),假如取定为i。

2. 则left subtree由min ~ i-1组成,假设可以有L种可能。right subtree由i+1 ~ max组成,假设有R种可能。生成所有可能的left/right subtree。

3 对于每个生成的left subtree/right subtree组合<T_left(p), T_right(q)>,p = 1...L,q = 1...R,添加上根节点i而组成一颗新树。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<TreeNode*> generateTrees(int n) {
        return fun(1,n);
    }
    vector<TreeNode*> fun(int min,int max){
        vector<TreeNode*> ret;
        if (min > max){
            ret.push_back(NULL);
            return ret;
        }
        for(int i = min; i <= max; i++){
            vector<TreeNode*> left = fun(min,i-1);
            vector<TreeNode*> right = fun(i+1, max);
            for (int m = 0; m < left.size(); m++){
                for(int n = 0; n <right.size(); n++){
                    TreeNode * root = new TreeNode(i);
                    root->left = left[m];
                    root->right = right[n];
                    ret.push_back(root);
                }
            }
        }
    }
};
时间: 2024-12-18 10:04:38

LeetCode: Unique Binary Search Trees I & II的相关文章

[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 -day28 Unique Binary Search Trees I II

1.  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 /

[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 [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个值,能构造多少棵合法的二叉

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}&