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

【分析】

参考:[LeetCode]96.Unique Binary Search Trees

【代码】

/*********************************
*   日期:2014-12-27
*   作者:SJF0115
*   题目: 95.Unique Binary Search Trees II
*   来源:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#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 generate(1, 0);
        }//if
        else{
            return generate(1, n);
        }
    }//
private:
    // 返回根节点结合
    vector<TreeNode*> generate(int start,int end){
        vector<TreeNode*> subTree;
        if(start > end){
            subTree.push_back(NULL);
            return subTree;
        }//if
        // i作为根节点
        for(int i = start;i <= end;i++){
            // 以i为根节点的树,其左子树由[start,i-1]构成,其右子树由[i+1,end]构成。
            // 返回的是不同二叉查找树的根节点,几种二叉查找树就返回几个根节点
            vector<TreeNode*> leftSubTree = generate(start,i-1);
            vector<TreeNode*> rightSubTree = generate(i+1,end);
            // 左子树右子树跟根节点连接
            // 以i为根的树的个数,等于左子树的个数乘以右子树的个数
            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);
                }//for
            }//for
        }//for
        return subTree;
    }//
};
// 先序遍历
void PreOrder(TreeNode* root){
    if(root == NULL){
        return;
    }//if
    cout<<root->val<<" ";
    PreOrder(root->left);
    PreOrder(root->right);
}

int main() {
    Solution solution;
    vector<TreeNode*> vec = solution.generateTrees(3);
    for(int i = 0;i < vec.size();i++){
        TreeNode* root = vec[i];
        PreOrder(root);
        cout<<endl;
    }
}

时间: 2024-11-08 19:19:09

[LeetCode]95.Unique Binary Search Trees II的相关文章

[LeetCode]96.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 [分析] 如果把上例的顺序改一下,就可以看出规律了. 比如,以 1 为根的树的个数

【LeetCode从零单排】No96 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 百度这个说的挺好:点击打开链接 代码 public class Solution { p

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  C++代码实现: #include<iostream> using namespace s

[LeetCode] Trim a Binary Search Tree 修剪一棵二叉搜索树

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary

[LeetCode]99.Recover Binary Search Tree

[题目] Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? [解析] O(n) 空间的解法是,开一个指针数组

Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

[LeetCode]235.Lowest Common Ancestor of a Binary Search Tree

题目 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes v and w as the lowest node in T that h

[LeetCode]173.Binary Search Tree Iterator

[题目] Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and

[LeetCode]109.Convert Sorted List to Binary Search Tree

[题目] Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. [分析] 无 [代码] 在下面超时的代码上进行改进:把链表先转换为vector再进行操作. [LeetCode]108.Convert Sorted Array to Binary Search Tree /*******************************