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

分析:本题很容易想到用递归的方法,依次将每个点作为根结点,然后递归左右子树,但是在这个递归中有个问题,如何判断递归结束,如何保留上层的结点,如何判断递归的是左子树还是有子树,递归的写法还是很重要的。本题中,采用每次先求得下层子树左右子树的结点,然后根据左右子树的不同情况形成不同的树,保存这些树的根结点。

代码如下:

class Solution {
public:
    vector<TreeNode *> generateTrees(int n) {
        return constructTree(0,n-1);
    }
    vector<TreeNode*> constructTree(int startIndex, int endIndex){
        vector<TreeNode *> result; //保存下一层的结点
        if(startIndex > endIndex){
            result.push_back(NULL);
            return result;
        }
        for(int i=startIndex; i<=endIndex; ++i){
            vector<TreeNode*> leftTrees = constructTree(startIndex,i-1);
            vector<TreeNode*> rightTrees = constructTree(i+1,endIndex);
            for(int j=0; j<leftTrees.size(); ++j){
                for(int k=0; k<rightTrees.size(); ++k){
                    TreeNode * newNode = new TreeNode(i+1);
                    result.push_back(newNode);
                    newNode->left = leftTrees[j];
                    newNode->right = rightTrees[k];

                }
            }
        }
        return result;
    }
};

2、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

分析:先做上面的题,后面这个题就简单了,采用递归,每次计算下层的左子树和右子树的数目,本层的不同树的个数为左子树的数目与右子树的数目相乘,每种不同的情况相加即可。

class Solution {
public:
    int numTrees(int n) {
        if(n < 1){
            return 1;
        }
        return numTrees(1,n);
    }
    int numTrees(int startIndex, int endIndex){
        int num = 1;
        if(startIndex > endIndex){
            return num;
        }
        num = 0;
        for(int i=startIndex; i<=endIndex; ++i){
            int leftNum = numTrees(startIndex,i-1);
            int rightNum = numTrees(i+1,endIndex);
            num += leftNum * rightNum;
        }
        return num;
    }
};

leetcode -day28 Unique Binary Search Trees I II,布布扣,bubuko.com

时间: 2024-08-03 15:26:05

leetcode -day28 Unique Binary Search Trees I II的相关文章

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

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Binary Search Trees Total Accepted: 13478 Total Submissions: 37858 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there

[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

【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

[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,#,

[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 ,生成所有的存在且唯一的

Unique Binary Search Trees I &amp; II

Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? 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,2,3,4,.. i,.. n时,基于以下原则的BST建树具有唯一性:以i为根节点的树,其

【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

【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.