Skip to content

Commit

Permalink
0096 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Sep 10, 2018
1 parent db9b9a3 commit 62bcfca
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 0096-Unique-Binary-Search-Trees/cpp-0096/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5)
project(cpp_0096)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main2.cpp)
add_executable(cpp_0096 ${SOURCE_FILES})
45 changes: 45 additions & 0 deletions 0096-Unique-Binary-Search-Trees/cpp-0096/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/// Source : https://leetcode.com/problems/unique-binary-search-trees/description/
/// Author : liuyubobobo
/// Time : 2018-09-10

#include <iostream>
#include <vector>

using namespace std;


/// Memory Search
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {

public:
int numTrees(int n) {

vector<int> dp(n + 1, -1);
return numTrees(n, dp);
}

private:
int numTrees(int n, vector<int>& dp){

if(n <= 1)
return 1;

if(dp[n] != -1)
return dp[n];

int res = 0;
for(int i = 1; i <= n; i ++)
res += numTrees(i - 1, dp) * numTrees(n - i, dp);
return dp[n] = res;
}
};


int main() {

cout << Solution().numTrees(3) << endl;

return 0;
}
34 changes: 34 additions & 0 deletions 0096-Unique-Binary-Search-Trees/cpp-0096/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// Source : https://leetcode.com/problems/unique-binary-search-trees/description/
/// Author : liuyubobobo
/// Time : 2018-09-10

#include <iostream>
#include <vector>

using namespace std;


/// Dynamic Programming
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {

public:
int numTrees(int n) {

vector<int> dp(n + 1, 0);
dp[0] = dp[1] = 1;
for(int i = 2; i <= n; i ++)
for(int j = 1; j <= i; j ++)
dp[i] += dp[j - 1] * dp[i - j];
return dp[n];
}
};


int main() {

cout << Solution().numTrees(3) << endl;

return 0;
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ email: [[email protected]](mailto:[email protected])
| | | | | | |
| 094 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/description/) | [solution](https://leetcode.com/problems/binary-tree-inorder-traversal/solution/) | [C++](0094-Binary-Tree-Inorder-Traversal/cpp-0094/) | [Java](0094-Binary-Tree-Inorder-Traversal/java-0094/src/) | |
| | | | | | |
| 096 | [Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/description/) | [] | [C++](0096-Unique-Binary-Search-Trees/cpp-0096/) | | |
| | | | | | |
| 098 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/description/) | [] | [C++](0098-Validate-Binary-Search-Tree/cpp-0098/) | [Java](0098-Validate-Binary-Search-Tree/java-0098/src/) | |
| 099 | [Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/description/) | [] | | [Java](0099-Recover-Binary-Search-Tree/java-0099/src/) | |
| | | | | | |
Expand Down

0 comments on commit 62bcfca

Please sign in to comment.