您的位置:首页 > 产品设计 > UI/UE

LeetCode题解——Unique Binary Search Trees II

2015-11-07 22:45 495 查看
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


想法一:递归,思想见Unique Binary Search Trees

class Solution {
public:
vector<TreeNode*> generateTrees(int n) {
return generateTrees(1,n);
}
vector<TreeNode*> generateTrees(int i, int j) {//建立sequence i,i+1,...,j的BST
vector<TreeNode*> temp;
TreeNode* root=NULL;
if(i>j){
temp.push_back(root);
return temp;
}
if(i==j){
root = new TreeNode(i);
temp.push_back(root);
return temp;
}
for(int m = i;m<=j;m++){
vector<TreeNode*> left = generateTrees(i,m-1);
vector<TreeNode*> right = generateTrees(m+1,j);
for(int l = 0;l<left.size();l++){
for(int r = 0; r<right.size();r++){
TreeNode* root = new TreeNode(m);
root->left = left[l];
root->right = right[r];
temp.push_back(root);
}
}
}
return temp;
}
};


思想二:DP ;

The basic idea is that we can construct the result of n node tree just from the result of n-1 node tree. Here's how we do it: only 2
conditions: 1) The nth node is the new root, so
newroot->left = oldroot;
2)
the nth node is not root, we traverse the old tree, every time the node in the old tree has a right child, we can perform:
old
node->right = nth node, nth node ->left = right child;
and when we reach the end of the tree, don't forget we can also add the
nth node here. One thing to notice is that every time we push a TreeNode in our result, I push the clone version of the root, and I recover what I do to the old node immediately. This is because you may use the old tree for several times.

/**
* Definition for binary tree
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
TreeNode * clone(TreeNode * root){
if(NULL == root) return NULL;
TreeNode * newRoot = new TreeNode(root->val);
newRoot->left = clone(root->left);
newRoot->right = clone(root->right);
return newRoot;
}
void addToLeft(TreeNode * oldRoot, TreeNode * newRoot, vector<TreeNode *> & results){
TreeNode * cloneRoot = clone(oldRoot);
newRoot->left = cloneRoot;
results.push_back(newRoot);
}
void addRight(TreeNode * oldRoot, TreeNode * cur, TreeNode * newNode, vector<TreeNode *> & results){
TreeNode *cloneRoot = clone(oldRoot);
TreeNode *newCur = cloneRoot;
while(NULL != newCur){
if(newCur->val == cur->val) break;
else newCur = newCur->right;
}
assert(NULL != newCur);
TreeNode * temp = newCur->right;
newCur->right = newNode;
newNode->left = temp;
results.push_back(cloneRoot);
}
public:
vector<TreeNode *> generateTrees(int n) {
// DP:
// for n-1 to n , for each n -1
// 1) left to n
// 2) for each right child down
//    n replace right and right will be n left
vector<TreeNode *> results;
vector<TreeNode *> preResults(1, NULL);
for(int i = 1; i <=n; i ++){
for(int j = 0; j < preResults.size(); j++){
// add n-1 left to n
TreeNode * oldRoot = preResults[j];
TreeNode * newRoot = new TreeNode(i);
addToLeft(oldRoot, newRoot, results);
TreeNode * cur = oldRoot;
while(NULL != cur){
TreeNode *newNode = new TreeNode(i);
addRight(oldRoot, cur, newNode, results);
cur = cur->right;
}
}
swap(results, preResults);
results.clear();
}
return preResults;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: