Leetcode 树 Unique Binary Search TreesII

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Unique Binary Search Trees II

Total Accepted: 7349 Total
Submissions: 27648

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.

题意:给定数字n,生成以数字1到n为节点的二叉查找树

思路:dfs暴力枚举

以i为根的树的左右树分别是[1,i-1]和[i+1,n]

递归函数:

vector<TreeNode *> generateTree(int begin, int end)

表示生成以[begin,end]的值为节点的二叉查找树

复杂度:不懂分析

相关题目: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 *> generateTrees(int begin, int end){
    	vector<TreeNode *> results;
    	if(begin > end){
    		results.push_back(NULL);
    		return results;
    	}

    	for(int i = begin; i <= end; i++){
    		vector<TreeNode *> left_trees = generateTrees(begin, i - 1);
    		vector<TreeNode *> right_trees = generateTrees(i + 1, end);
    		for(int l = 0; l < left_trees.size(); l++){
    			for(int r = 0; r < right_trees.size(); r++){
    				TreeNode *head = new TreeNode(i);
    				head->left = left_trees[l];
    				head->right = right_trees[r];
    				results.push_back(head);
    			}
    		}
    	}

    }

    vector<TreeNode *> generateTrees(int n){
    	return generateTrees(1, n);
    }
};

Leetcode 树 Unique Binary Search TreesII

时间: 2024-11-03 13:46:28

Leetcode 树 Unique Binary Search TreesII的相关文章

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系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉排序树. 举个栗子,给定 n = 3, 共有 5 个. 1 3 3 2 1 \ / / / \ 3 2 1 1

[LeetCode]Unique Binary Search TreesII

题目:Unique Binary Search TreesII 如果要列出所有可能的二叉搜索树,可以在上面的思路上进一步. f(n) = f(0)*f(n-1) + f(1)*f(n-2) + ... + f(n-1)*f(0); 只要求出不同变量下的子树的所有情况,在整合到一起就可以了. 具体思路: 1.外循环遍历树根可能数值k(m->n); 2.分别求左右子树,左子树的可能取值范围(m->k-1),右子树的可能取值范围(k+1->n): 注意左右子树可能为空,此时后面合并的时候要分开

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 /

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

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

【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