95. Unique Binary Search Trees I II
in LeetCodeAlgorithms with 0 comment

95. Unique Binary Search Trees I II

in LeetCodeAlgorithms with 0 comment

95 Unique Binary Search Trees II

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

思路:
1 根节点可以任取 start ~ end (例如 start = 1, end = n),假如取定为i。
2 则left subtree由 start ~ i-1组成,假设可以有L种可能。right subtree由i+1 ~ end 组成,假设有R种可能。生成所有可能的left/right subtree。
3 对于每个生成的left subtree/right subtree组合<T_left(p), T_right(q)>,p = 1...L,q = 1...R,添加上根节点i而组成一颗新树。

#include<iostream>
#include<vector>
using namespace std;

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) {
        if (n == 0) return{};
        return *generateTreesDFS(1, n);
    }
    vector<TreeNode*> *generateTreesDFS(int start, int end) {
        vector<TreeNode*> *subTree = new vector<TreeNode*>();
        if (start > end) subTree->push_back(NULL);
        else {
            for (int i = start; i <= end; ++i) {//分别取第i个做root
                vector<TreeNode*> *leftSubTree = generateTreesDFS(start, i - 1);
                vector<TreeNode*> *rightSubTree = generateTreesDFS(i + 1, end);
                for (int j = 0; j < leftSubTree->size(); ++j) {
                    for (int k = 0; k < rightSubTree->size(); ++k) {
                        TreeNode *node = new TreeNode(i);
                        node->left = (*leftSubTree)[j];
                        node->right = (*rightSubTree)[k];
                        subTree->push_back(node);
                    }
                }
            }
        }
        return subTree;
    }
};

int main(){
    Solution test;
    vector<TreeNode*> answer;
    answer = test.generateTrees(3);
    return 0;
}

96 Unique Binary Search Trees I

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

首先注意这里是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)

#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    int numTrees(int n) {
        vector<int> data(n + 1, 0);
        data[0] = 1;
        for (int i = 1; i < n; i++) {//每个数字算过来
            for (int j = 0; i < i; j++) {
                data[i] += data[j] * data[i - j - 1];
            }
        }
        return data[n - 1];
    }
};
int main() {
    Solution test;
    cout << test.numTrees(3) << endl;
    return 0;
}
Responses

From now on, bravely dream and run toward that dream.
陕ICP备17001447号