【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[n]存储1-n的所有方法的根节点

则 f[n+1] = 1作为根,f[0]做左子树,f[n]所有节点都加1做右子树  +  2作为根,f[1]做左子树,f[n - 1]所有节点都加2做右子树 +...

代码内存都没有释放,不过AC了。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

// 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) {
        vector<vector<TreeNode *>> ans(n + 1, vector<TreeNode *>());
        if(n == 0)
        {
            ans[0].push_back(NULL);
            return ans[0];
        }

        TreeNode * root = NULL;
        ans[0].push_back(root);
        root = new TreeNode(1);
        ans[1].push_back(root);
        for(int i = 2; i <= n; i++) //总数字
        {
            for(int j = 0; j < i; j++) //小于根节点的数字个数
            {
                for(int l = 0; l < ans[j].size(); l++) //小于根节点的组成方法数
                {
                    for(int r = 0; r < ans[i - j - 1].size(); r++) //大于根节点的组成方法数
                    {
                        TreeNode * root = new TreeNode(j + 1);
                        root->left = ans[j][l];
                        root->right = add(ans[i - j - 1][r], j + 1); //大于根节点的需要加上差值
                        ans[i].push_back(root);
                    }
                }
            }
        }

        return ans[n];
    }

    TreeNode * add(TreeNode * root, int Num)
    {
        if(root == NULL)
        {
            return root;
        }
        TreeNode * T = new TreeNode(root->val + Num);
        T->left = add(root->left, Num);
        T->right = add(root->right, Num);

        return T;
    }
};

int main()
{
    Solution s;
    vector<TreeNode *> ans = s.generateTrees(3);

    return 0;
}

看别人的思路,把建立子树分为从数字start-end,直接递归求解,不需要像我的那样每次还要把右子树遍历增加值

/**
 * 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*> generateTreesRec(int start, int end){
        vector<TreeNode*> v;
        if(start > end){
            v.push_back(NULL);
            return v;
        }
        for(int i = start; i <= end; ++i){
            vector<TreeNode*> left = generateTreesRec(start, i - 1);
            vector<TreeNode*> right = generateTreesRec(i + 1, end);
            TreeNode *node;
            for(int j = 0; j < left.size(); ++j){
                for(int k = 0; k < right.size(); ++k){
                    node = new TreeNode(i);
                    node->left = left[j];
                    node->right = right[k];
                    v.push_back(node);
                }
            }
        }
        return v;
    }
    vector<TreeNode *> generateTrees(int n) {
        return generateTreesRec(1, n);
    }
};
				
时间: 2024-10-11 07:54:43

【leetcode】 Unique Binary Search Trees II (middle)☆的相关文章

【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】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】Unique Binary Search Trees (2 solutions)

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 Base case: n==0, n==1时,f

【树】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 思路: 找到一个数作为根结点,剩余的数分别

【leetcode】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. 注意:二分查找树的定义是,左子树节点均小于root,右子树节点均大于root! 不要想当然地将某个点作为root时,认为其他所有节点都能全部放在left/right中,除非这个点是 min 或者

【leetcode】Unique Binary Search Trees (#96)

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  

【leetcode】 Unique Binary Search Trees (middle)☆

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 找数字连续最大乘积子序列. 思路:这个麻烦在有负数和0,我的方法,如果有0,一切都设

【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~n的每个节点为根节点,然后递归

[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