Leetcode 95. Unique Binary Search Trees II

95. Unique Binary Search Trees II

  • Total Accepted: 61324
  • Total Submissions: 207585
  • Difficulty: Medium

 

Given an integer 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

 

 

思路:

方法一:DP。首先注意n=0的情况;当n>=1时,假设n个结点的二叉查找树的根结点为root(n)。对于val==n的d单个结点f(n),root(n)可以通过以下2种方式形成:

1.  f(n)->left=root(n)

2.  curTree.size()=n-1

while(f(n)) 

  f(n)->left=root(subRightTree.size())

  root(curTree.size())->right=f(n)

  记录root(n),恢复root(n)

  当前子树指向其右子树

 

不断遍历右子树,直到右子树为空

 

方法二:分而治之。

 

 

 

这里亲测测试数据中有n=0的测试,并且n=0时,要返回[],而不是[[]]

 

代码:

方法一:

 1 /**
 2  * Definition for a binary tree node.
 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     TreeNode* clone(TreeNode* root){
13         if(!root) return NULL;
14         TreeNode* res=new TreeNode(root->val);
15         res->left=clone(root->left);
16         res->right=clone(root->right);
17         return res;
18     }
19     vector<TreeNode*> generateTrees(int n) {
20         vector<TreeNode*> res;
21         if(!n) return res;
22         TreeNode *cur=new TreeNode(1),*head,*pre,*tail;
23         res.push_back(cur);
24         for(int i=2;i<=n;i++){
25             int size=res.size();
26             for(int j=0;j<size;j++){
27                 cur=new TreeNode(i);
28                 head=res[0];
29                 res.erase(res.begin());
30                 pre=head;
31                 while(pre!=NULL){
32                     tail=pre->right;
33                     pre->right=cur;
34                     cur->left=tail;
35                     res.push_back(clone(head));
36                     cur->left=NULL;
37                     pre->right=tail;
38                     pre=tail;
39                 }
40                 cur->left=head;
41                 res.push_back(cur);
42             }
43         }
44         return res;
45     }
46 };

 

方法二:

 1 class Solution {
 2 public:
 3     vector<TreeNode*> generateTree(int begin,int end){
 4         vector<TreeNode*> left,right,res;
 5         if(begin>end){
 6             res.push_back(NULL);
 7             return res;
 8         }
 9         for(int i=begin;i<=end;i++){
10             left=generateTree(begin,i-1);
11             right=generateTree(i+1,end);
12             for(auto lnode:left){
13                 for(auto rnode:right){
14                     TreeNode* root=new TreeNode(i);
15                     root->left=lnode;
16                     root->right=rnode;
17                     res.push_back(root);
18                 }
19             }
20         }
21         return res;
22     }
23     vector<TreeNode*> generateTrees(int n) {
24         if(!n) return vector<TreeNode*>();
25         return generateTree(1,n);
26     }
27 };

 

posted @ 2016-08-09 15:01  Deribs4  阅读(380)  评论(0编辑  收藏  举报