-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
uniqueBinarySearchTrees.II.cpp
110 lines (99 loc) · 2.56 KB
/
uniqueBinarySearchTrees.II.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Source : https://oj.leetcode.com/problems/unique-binary-search-trees-ii/
// Author : Hao Chen
// Date : 2014-06-25
/**********************************************************************************
*
* 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
*
* confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
*
* OJ's Binary Tree Serialization:
*
* The serialization of a binary tree follows a level order traversal, where '#' signifies
* a path terminator where no node exists below.
*
* Here's an example:
*
* 1
* / \
* 2 3
* /
* 4
* \
* 5
*
* The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
*
*
**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
vector<TreeNode*> generateTrees(int low, int high);
vector<TreeNode*> generateTrees(int n) {
vector<TreeNode*> v;
v = generateTrees(1, n);
return v;
}
vector<TreeNode*> generateTrees(int low, int high){
vector<TreeNode*> v;
if (low > high || low<=0 || high<=0){
v.push_back(NULL);
return v;
}
if (low==high){
TreeNode* node = new TreeNode(low);
v.push_back(node);
return v;
}
for (int i=low; i <= high; i++){
vector<TreeNode*> vleft = generateTrees(low, i-1);
vector<TreeNode*> vright = generateTrees(i+1, high);
for (int l=0; l<vleft.size(); l++){
for (int r=0; r<vright.size(); r++){
TreeNode *root = new TreeNode(i);
root->left = vleft[l];
root->right = vright[r];
v.push_back(root);
}
}
}
return v;
}
void printTree(TreeNode *root){
if (root == NULL){
printf("# ");
return;
}
printf("%d ", root->val );
printTree(root->left);
printTree(root->right);
}
int main(int argc, char** argv)
{
int n=2;
if (argc>1){
n = atoi(argv[1]);
}
vector<TreeNode*> v = generateTrees(n);
for(int i=0; i<v.size(); i++){
printTree(v[i]);
printf("\n");
}
return 0;
}