95. Unique Binary Search Trees II
来源:互联网 发布:浙江华通云数据上市 编辑:程序博客网 时间:2024/05/16 18:33
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ //[3,1,4,null,2,null,5],[3,1,5,null,2,4],[3,2,4,1,null,null,5],[3,2,5,1,null,4], //failedclass Solution {public: TreeNode* copy_tree(TreeNode* root) { if(!root) return NULL; TreeNode* temp=new TreeNode(root->val); temp->left=copy_tree(root->left); temp->right=copy_tree(root->right); return temp; } int inorder(TreeNode* root,int i) { if(root->left) i=inorder(root->left,i); root->val=i++; if(root->right) i=inorder(root->right,i); return i; } void get_tree(vector<TreeNode*>& v,TreeNode* root,TreeNode* temp,int i,int n) { if(i>n) { inorder(root,1); v.push_back(copy_tree(root)); //if(v.size()==3) cout<<root->right->right->val; //cout<<root->left->left->val; //v.push_back(root); return; } TreeNode* tmp=new TreeNode(i); if(temp->left) // { temp->right=tmp; get_tree(v,root,temp->left,i+1,n); if(i<n) get_tree(v,root,temp->right,i+1,n); temp->right=NULL; } else { temp->left=tmp; get_tree(v,root,tmp,i+1,n); if(i<n) get_tree(v,root,temp,i+1,n); temp->left=NULL; temp->right=tmp; get_tree(v,root,tmp,i+1,n); temp->right=NULL; } delete tmp; } vector<TreeNode*> generateTrees(int n) { if(n==0) return {}; vector<TreeNode*> v; TreeNode* tmp=new TreeNode(1); get_tree(v,tmp,tmp,2,n); return v; }};
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ //failedclass Solution {public: TreeNode* copy_tree(TreeNode* root) { if(!root) return NULL; TreeNode* temp=new TreeNode(root->val); temp->left=copy_tree(root->left); temp->right=copy_tree(root->right); return temp; } void inorder(TreeNode* root) { if(root->left) inorder(root->left); root->val++; if(root->right) inorder(root->right); } void outorder(TreeNode* root) { if(root->left) outorder(root->left); root->val--; if(root->right) outorder(root->right); } void get_tree(vector<TreeNode*>& v,TreeNode* root,TreeNode* temp,int i,int n) { if(i>n) { //inorder(root,1); v.push_back(copy_tree(root)); return; } TreeNode* tmp=new TreeNode(i); //make it the root tmp->left=root; get_tree(v,tmp,tmp,i+1,n); tmp->left=NULL; if(temp->left&&(temp->left->left||temp->left->right)) { inorder(root); tmp->val=1; tmp->right=root; get_tree(v,tmp,temp,i+1,n); tmp->right=NULL; if(!root->left) { root->left=tmp; get_tree(v,root,temp,i+1,n); root->left=NULL; } tmp->val=i; outorder(root); } //make it the rightest temp->right=tmp; get_tree(v,root,tmp,i+1,n); temp->right=NULL; if(!temp->left&&temp!=root) { temp->left=tmp; temp->val=i; tmp->val=i-1; get_tree(v,root,temp,i+1,n); temp->left=NULL; temp->val=i-1; } delete tmp; } vector<TreeNode*> generateTrees(int n) { if(n==0) return {}; vector<TreeNode*> v; TreeNode* tmp=new TreeNode(1); get_tree(v,tmp,tmp,2,n); return v; }};
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<TreeNode*> get_tree(int l,int r) { if(l>r) return {NULL}; vector<TreeNode*> v; for(int i=l;i<=r;i++) { vector<TreeNode*> left=get_tree(l,i-1); vector<TreeNode*> right=get_tree(i+1,r); for(auto a=left.begin();a!=left.end();a++) { for(auto b=right.begin();b!=right.end();b++) { TreeNode* temp=new TreeNode(i); temp->left=*a; temp->right=*b; v.push_back(temp); } } } return v; } vector<TreeNode*> generateTrees(int n) { if(n==0) return {}; return get_tree(1,n); }};
0 0
- [LeetCode]95.Unique Binary Search Trees II
- [Leetcode] 95. Unique Binary Search Trees II
- LeetCode --- 95. Unique Binary Search Trees II
- [leetcode] 95.Unique Binary Search Trees II
- [leetcode] 95. Unique Binary Search Trees II
- 95. Unique Binary Search Trees II
- 95. Unique Binary Search Trees II LeetCode
- ***LeetCode 95. Unique Binary Search Trees II
- 95. Unique Binary Search Trees II
- 95. Unique Binary Search Trees II
- 95. Unique Binary Search Trees II
- Leetcode 95. Unique Binary Search Trees II
- 95. Unique Binary Search Trees II
- LeetCode *** 95. Unique Binary Search Trees II
- 【LeetCode】95. Unique Binary Search Trees II
- 95. Unique Binary Search Trees II
- LeetCode 95. Unique Binary Search Trees II
- 95. Unique Binary Search Trees II
- 分类模型的性能评估——以SAS Logistic回归为例(3): Lift和Gain
- android重新启动应用程序和重新启动系统
- hadoop 2.4编译、安装
- C++:用sprintf()快速把想要的东西转成string类
- WebViewJavascriptBridge学习
- 95. Unique Binary Search Trees II
- javaScript树形结构
- 2016sdau课程练习专题一 1013 problemN
- Android ndk使用介绍
- iOS的cookie认证登陆
- js中for in 和 for each in的使用
- Hive建立外部表与时间戳转换(含建dual表,修改列名,row_number() 函数等)
- Bezier曲线原理
- 有趣的数