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;
int ans = 0;
for (int i = 1; i <= n; ++i) {
const int left = numTrees(i - 1);
const int right = numTrees(n - i);
ans += left * right;
}
return ans;
}
};
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.
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 \ 5The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}"
.
class Solution {
vector<TreeNode*> build(int left, int right) {
vector<TreeNode*> res;
if (left > right) {
res.push_back(NULL);
return res;
}
else if (left == right) {
TreeNode *p = new TreeNode(left);
res.push_back(p);
return res;
}
for (int i = left; i <= right; ++i) {
vector<TreeNode*> l = build(left, i - 1);
vector<TreeNode*> r = build(i + 1, right);
const int m = l.size(), n = r.size();
for (int j = 0; j < m; ++j) {
for (int k = 0; k < n; ++k) {
TreeNode *p = new TreeNode(i);
p->left = l[j];
p->right = r[k];
res.push_back(p);
}
}
}
return res;
}
public:
vector<TreeNode*> generateTrees(int n) {
return build(1, n);
}
};
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
class Solution {
map<pair<int, int>, int> memo;
int C(int n, int r) {
if (r > n - r) r = n - r;
if (r == 0) return 1;
if (r == 1) return n;
pair<int, int> p = make_pair(n, r);
if (memo.find(p) != memo.end())
return memo[p];
return (memo[p] = C(n - 1, r) + C(n - 1, r - 1));
}
public:
int uniquePaths(int m, int n) {
return C(m + n - 2, m - 1);
}
};
Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1
and 0
respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[ [0,0,0], [0,1,0], [0,0,0] ]
The total number of unique paths is 2
.
Note: m and n will be at most 100.
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
const int m = obstacleGrid.size();
if (m == 0) return 0;
const int n = obstacleGrid[0].size();
vector<vector<int> > dp;
for (int i = 0; i < m; ++i)
dp.push_back(vector<int>(n, 0));
if (obstacleGrid[0][0] == 0)
dp[0][0] = 1;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (obstacleGrid[i][j] == 1) {
dp[i][j] = 0;
continue;
}
if (i > 0) dp[i][j] += dp[i - 1][j];
if (j > 0) dp[i][j] += dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
};