LeetCode (U)

Unique Binary Search Trees

  Total Accepted: 3880  Total Submissions: 11207 My Submissions

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;
        }
};



Unique Binary Search Trees II

  Total Accepted: 2016  Total Submissions: 7973 My Submissions

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
    \
     5
The 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);
        }
};




Unique Paths

  Total Accepted: 3209  Total Submissions: 10652 My Submissions

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);
        }
};




Unique Paths II

  Total Accepted: 2210  Total Submissions: 8467 My Submissions

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];
        }
};






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值