Answers for "finding unique BSTs"

C++
0

finding unique BSTs

#define vN vector<Node *>

class Node{
public:
    long val = 0;
    Node *left = nullptr, *right = nullptr;
    Node(int val_): val(val_){}
};

vN find_unique_BSTs(long start, long end){
    vN res = {};

    if(start > end){
        res.push_back(nullptr);
        return res;
    }

    for(long i = start; i <= end; ++i){
        vN left = find_unique_BSTs(start, i-1);
        vN right = find_unique_BSTs(i+1, end);
        for(Node *l: left){
            for(Node *r: right){
                Node *root = new Node(i);
                root->left = l;
                root->right = r;
                res.push_back(root);
            }
        }
    }
    return res;
}

vN res = find_unique_BSTs(1,n);
Posted by: Guest on August-06-2021

Code answers related to "finding unique BSTs"

Browse Popular Code Answers by Language