[C++]LeetCode: 92 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}" means? >
read more on how binary tree is serialized on OJ.

思路:这道题是求解所有可行的二叉查找树,从Unique Binary Search Trees中我们已经知道如何得到所有可行的二叉查找树。我们每次从1~n中遍历选取一个点i作为树的根,则左子树的组合numTrees(left)
和右子树的组合numTrees(right)乘积即为当前根的组合数。根据二叉查找树的定义,左子树结点个数为(i-1),右子树为(n-i)。思路是构造一个辅助函数,我们每次一次选取一个结点为根,然后递归求解此根的左右子树的所有结果。最后再根据左右子树的返回子树,依次选取接到根结点上。构造好之后在作为当前树的结果返回。

这道题思维上一个难点,就是如何连接所有的左右子树和根结点,我们这里将递归放进循环里面,采用这个技巧,可以抽离根结点和左右子树的节点,同时递归产生的仍然是左右子树的根结点。另外一点,如何设计递归结束条件,当start > end时,说明超出范围,递归结束,不存在该结点,push(NULL)

Attention:

1. 设置递归终止条件和特殊情况。

if(start > end)
        {
            trees.push_back(NULL);
            return trees;
        }

        if(start == end)
        {
            trees.push_back(new TreeNode(start));
            return trees;
        }

2. trick: 将递归放进循环中,抽离根结点和左右子树的节点

//产生以i为根结点的左右子树的所有可能。i取值start到end
            vector<TreeNode*> leftTree = generateTrees_helper(start, i-1);
            vector<TreeNode*> rightTree = generateTrees_helper(i+1, end);

3. 如何连接根结点和左右子树,左右子树递归后返回的仍是左右子树的根结点,分别接到当前节点的左孩子和右孩子上,再把当前节点添加到树中。

TreeNode* root = new TreeNode(i);
                    root->left = leftTree[j];
                    root->right = rightTree[k];
                    trees.push_back(root);  //trees保存当前的根结点

4. 注意连接时,可行数= 左子树 * 右子树,注意循环条件,左子树的所有子树 * 右子树的所有子树。

5. 我们的辅助函数,始终维护的是根结点。

复杂度:NP问题

AC Code:

/**
 * 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 *> generateTrees(int n) {
       return generateTrees_helper(1, n);
    }

private:
    vector<TreeNode*> generateTrees_helper(int start, int end)
    {
        vector<TreeNode*> trees;
        if(start > end)
        {
            trees.push_back(NULL);
            return trees;
        }

        if(start == end)
        {
            trees.push_back(new TreeNode(start));
            return trees;
        }

        for(int i = start; i <= end; i++)
        {
            //产生以i为根结点的左右子树的所有可能。i取值start到end
            vector<TreeNode*> leftTree = generateTrees_helper(start, i-1);
            vector<TreeNode*> rightTree = generateTrees_helper(i+1, end);

            //将左子树的所有可能和右子树的所有可能连接.leftTree[0]表示第一个节点。
            for(int j = 0; j < leftTree.size(); j++)
            {
                for(int k = 0; k < rightTree.size(); k++)
                {
                    TreeNode* root = new TreeNode(i);
                    root->left = leftTree[j];
                    root->right = rightTree[k];
                    trees.push_back(root);  //trees保存当前的根结点
                }
            }
        }

        return trees;
    }
};
时间: 2024-10-13 23:07:44

[C++]LeetCode: 92 Unique Binary Search Trees II的相关文章

[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

Java for LeetCode 095 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. 解题思路: 参考Java for LeetCode 096 Unique Binary Search Trees思路,本题很容易解决.注意,

【LeetCode】Unique Binary Search Trees II 不同的二叉查找树II

今早起来做 LeetCode,结果被这道题卡了将近1个半小时,忍着没有去搜答案,最后还是被我想出来了,而且直接一次AC,哈哈!现在在这里记录一下解题思路. 原题: 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 sh

[LeetCode][Java]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 题意: 给定n ,生成所有的存在且唯一的

【Leetcode】Unique Binary Search Trees II

题目链接:https://leetcode.com/problems/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.

leetCode 95.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 (middle)☆

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 这次的题目要求是得到所有的树. 我的思路: 用f[

[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 /