-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique_binary_search_trees2.cpp
More file actions
83 lines (77 loc) · 2.09 KB
/
unique_binary_search_trees2.cpp
File metadata and controls
83 lines (77 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* =====================================================================================
*
* Filename: unique_binary_search_trees2.cpp
*
* Description: 95. Unique Binary Search Trees II.
* Given an integer n, generate all structurally unique BST's (binary
* search trees) that store values 1 ... n.
*
* Version: 1.0
* Created: 05/03/19 03:39:34
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <vector>
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
std::vector<TreeNode*> generateTrees(int n)
{
if (n < 1)
{
return std::vector<TreeNode*>();
}
return buildTrees(1, n);
}
private:
std::vector<TreeNode*> buildTrees(int start, int end)
{
if (start > end)
{
return std::vector<TreeNode*>(1, (TreeNode*)NULL);
}
else if (start == end)
{
TreeNode* node = new TreeNode(start);
return std::vector<TreeNode*>(1, node);
}
std::vector<TreeNode*> trees;
for (int i = start; i <= end; i++)
{
auto left_trees = buildTrees(start, i - 1);
auto right_trees = buildTrees(i + 1, end);
for (auto& left_node: left_trees)
{
for (auto& right_node: right_trees)
{
TreeNode* cur_root = new TreeNode(i);
cur_root->left = left_node;
cur_root->right = right_node;
trees.push_back(cur_root);
}
}
}
return trees;
}
};
int main(int argc, char* argv[])
{
auto trees = Solution().generateTrees(3);
printf("Total number of trees: %zd\n", trees.size());
return 0;
}