LeetCode Unique Binary Search Trees 2 递归方法
来源:互联网 发布:郑元畅为什么不红 知乎 编辑:程序博客网 时间:2024/06/07 12:13
//递归方法解决
//I start by noting that 1..n is the in-order traversal for any BST with nodes 1 to n.
//So if I pick i-th node as my root, the left subtree will contain elements 1 to (i-1),
//and the right subtree will contain elements (i+1) to n.
//I use recursive calls to get back all possible trees for left and right subtrees
//and combine them in all possible ways with the root.
/************************************************************************************ 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**********************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <vector>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};vector<TreeNode*> generateTrees(int low, int high);vector<TreeNode*> generateTrees(int n) { vector<TreeNode*> v; if (n==0) return v; //i find add this can ac ,if do not add it is return wrong. v = generateTrees(1, n); return v;}vector<TreeNode*> generateTrees(int low, int high) { vector<TreeNode*> v; if (low > high || low <= 0 || high <= 0) { v.push_back(NULL); return v; } if (low == high) { TreeNode* node = new TreeNode(low); v.push_back(node); return v; } //the i is root,and construct the left subtree and right subtree. for (int i = low; i <= high; i++) { vector<TreeNode*> vleft = generateTrees(low, i - 1); vector<TreeNode*> vright = generateTrees(i + 1, high); for (int l = 0; l<vleft.size(); l++) { for (int r = 0; r<vright.size(); r++) { TreeNode *root = new TreeNode(i); root->left = vleft[l]; root->right = vright[r]; v.push_back(root); } } } return v;}void printTree(TreeNode *root) { if (root == NULL) { printf("# "); return; } printf("%d ", root->val); printTree(root->left); printTree(root->right);}int main(int argc, char** argv){ int n = 2; vector<TreeNode*> v = generateTrees(n); for (int i = 0; i<v.size(); i++) { printTree(v[i]); printf("\n"); } return 0;}
0 0
- LeetCode Unique Binary Search Trees 2 递归方法
- LeetCode: Unique Binary Search Trees
- LeetCode : Unique Binary Search Trees
- [Leetcode] Unique Binary Search Trees
- [LeetCode]Unique Binary Search Trees
- LeetCode Unique Binary Search Trees
- Leetcode: Unique Binary Search Trees
- LeetCode-Unique Binary Search Trees
- [leetcode] Unique Binary Search Trees
- LeetCode - Unique Binary Search Trees
- [LeetCode] Unique Binary Search Trees
- 【Leetcode】Unique Binary Search Trees
- Leetcode: Unique Binary Search Trees
- 【LeetCode】Unique Binary Search Trees
- Leetcode Unique Binary Search Trees
- Leetcode: Unique Binary Search Trees
- [LeetCode] Unique Binary Search Trees
- Leetcode: Unique Binary Search Trees
- DHCP与PPPOE 区别
- poj1041 John's trip (欧拉回路)
- lintcode construct-binary-tree-from-inorder-and-postorder-traversal 中序遍历和后序遍历树构造二叉树
- HttpURLConnection与 HttpClient 区别
- 牛顿法与拟牛顿法
- LeetCode Unique Binary Search Trees 2 递归方法
- geekband android #5 第一周分享(初识Android)
- 无法执行添加/移除操作,因为代码元素**是只读的
- oracle的分析函数over及开窗关键字range
- 【SSH进阶之路】Spring的IOC逐层深入——依赖注入的两种实现类型
- hdu1198 Farm Irrigation (DFS)
- linux日志文件
- getchar和scanf的区别
- IOS Dev Intro - AVASSetWriterInput Scale Encode