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

【题意】

Unique Binary Search Trees只需要让我们求出可能二叉搜索树的数目,而本题是让我们把这些可能的二叉搜索树都建出来。

【思路】

思路和Unique Binary Search Trees完全相同,将原问题分解为求解左右子树所有可能形态的子问题,用递归求解。只不过多了建树的环节。

【代码】

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<TreeNode *> getAllBinaryTrees(int start, int end){
        vector<TreeNode*> trees;
        if(start>end){              //注意这里没有等号,Unique Binary Search Trees只是计数,叶子节点和NULL节点都计数为1, 而本题就不一样了,叶子节点作为一棵独立的子树生成。
            trees.push_back(NULL);
            return trees;
        }

        for(int k=start; k<=end; k++){
            //得到左子树集合
            vector<TreeNode*>leftTrees = getAllBinaryTrees(start, k-1);
            //得到右子树集合
            vector<TreeNode*>rightTrees = getAllBinaryTrees(k+1, end);

            //组合,生成二叉搜索树
            for(int l=0; l<leftTrees.size(); l++){
                for(int r=0; r<rightTrees.size(); r++){
                    //创建根节点
                    TreeNode *root = (TreeNode*)malloc(sizeof(TreeNode));
                    root->val=k;
                    root->left=leftTrees[l];
                    root->right=rightTrees[r];
                    trees.push_back(root);
                }
            }
        }
        return trees;
    }

    vector<TreeNode *> generateTrees(int n) {
        vector<TreeNode*>result;
        if(n==0){result.push_back(NULL);return result;}      //当n==0时是一棵空树,别忘了
        result = getAllBinaryTrees(1, n);
        return result;
    }
};

LeetCode: Unique Binary Search Trees II [096]

时间: 2024-12-21 13:46:00

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

[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 解题报告

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

[LeetCode] Unique Binary Search Trees II dfs 深度搜索

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][JavaScript]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 \ / / / \ 3 2 1 1 3 2 / / \ 2 1

Unique Binary Search Trees II leetcode java

题目: 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难的就是不是返回个数,而