本博客rss订阅地址: http://feed.cnblogs.com/blog/u/147990/rss

LeetCode:Unique Binary Search Trees I II

LeetCode:Unique Binary Search Trees 

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

分析:依次把每个节点作为根节点,左边节点作为左子树,右边节点作为右子树,那么总的数目等于左子树数目*右子树数目,实际只要求出前半部分节点作为根节点的树的数目,然后乘以2(奇数个节点还要加上中间节点作为根的二叉树数目)

递归代码:为了避免重复计算子问题,用数组保存已经计算好的结果

 1 class Solution {
 2 public:
 3     int numTrees(int n) {
 4         // IMPORTANT: Please reset any member data you declared, as
 5         // the same Solution instance will be reused for each test case.
 6         int nums[n+1]; //nums[i]表示i个节点的二叉查找树的数目
 7         memset(nums, 0, sizeof(nums));
 8         return numTreesRecur(n, nums);
 9     }
10     int numTreesRecur(int n, int nums[])
11     {
12         if(nums[n] != 0)return nums[n];
13         if(n == 0){nums[0] = 1; return 1;}
14         int tmp = (n>>1);
15         for(int i = 1; i <= tmp; i++)
16         {
17             int left,right;
18             if(nums[i-1])left = nums[i-1];
19             else left = numTreesRecur(i-1, nums);
20             if(nums[n-i])right = nums[n-i];
21             else right = numTreesRecur(n-i, nums);
22             nums[n] += left*right;
23         }
24         nums[n] <<= 1;
25         if(n % 2 != 0)
26         {
27             int val;
28             if(nums[tmp])val = nums[tmp];
29             else val = numTreesRecur(tmp, nums);
30             nums[n] += val*val;
31         }
32         return nums[n];
33     }
34 };

非递归代码:从0个节点的二叉查找树数目开始自底向上计算,dp方程为nums[i] = sum(nums[k-1]*nums[i-k]) (k = 1,2,3...i)

 1 class Solution {
 2 public:
 3     int numTrees(int n) {
 4         // IMPORTANT: Please reset any member data you declared, as
 5         // the same Solution instance will be reused for each test case.
 6         int nums[n+1]; //num[i]表示i个节点的二叉查找树数目
 7         memset(nums, 0, sizeof(nums));
 8         nums[0] = 1;
 9         for(int i = 1; i <= n; i++)
10         {
11             int tmp = (i>>1);
12             for(int j = 1; j <= tmp; j++)
13                 nums[i] += nums[j-1]*nums[i-j];
14             nums[i] <<= 1;
15             if(i % 2 != 0)
16                 nums[i] += nums[tmp]*nums[tmp];
17         }
18         return nums[n];
19     }
20 };

 

LeetCode:Unique Binary Search Trees II

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

按照上一题的思路,我们不仅仅要保存i个节点对应的BST树的数目,还要保存所有的BST树,而且1、2、3和4、5、6虽然对应的BST数目和结构一样,但是BST树是不一样的,因为节点值不同。

我们用数组btrees[i][j][]保存节点i, i+1,...j-1,j构成的所有二叉树,从节点数目为1的的二叉树开始自底向上最后求得节点数目为n的所有二叉树                                                                               本文地址

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<TreeNode *> generateTrees(int n) {
13         // IMPORTANT: Please reset any member data you declared, as
14         // the same Solution instance will be reused for each test case.
15         vector<vector<vector<TreeNode*> > > btrees(n+2, vector<vector<TreeNode*> >(n+2, vector<TreeNode*>()));
16         for(int i = 1; i <= n+1; i++)
17             btrees[i][i-1].push_back(NULL); //为了下面处理btrees[i][j]时 i > j的边界情况
18         for(int k = 1; k <= n; k++)//k表示节点数目
19             for(int i = 1; i <= n-k+1; i++)//i表示起始节点
20             {
21                 for(int rootval = i; rootval <= k+i-1; rootval++)
22                 {//求[i,i+1,...i+k-1]序列对应的所有BST树
23                     for(int m = 0; m < btrees[i][rootval-1].size(); m++)//左子树
24                         for(int n = 0; n < btrees[rootval+1][k+i-1].size(); n++)//右子树
25                         {
26                             TreeNode *root = new TreeNode(rootval);
27                             root->left = btrees[i][rootval-1][m];
28                             root->right = btrees[rootval+1][k+i-1][n];
29                             btrees[i][k+i-1].push_back(root);
30                         }
31                 }
32             }
33         return btrees[1][n];
34     }
35 };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3448569.html

posted @ 2013-11-28 22:09  tenos  阅读(1713)  评论(0编辑  收藏  举报

本博客rss订阅地址: http://feed.cnblogs.com/blog/u/147990/rss

公益页面-寻找遗失儿童