算法分析与设计第五周习题:95. Unique Binary Search Trees II
来源:互联网 发布:java中的访问修饰符 编辑:程序博客网 时间:2024/05/22 21:44
与96题的对比:
95题是96题Unique Binary Search Trees的升级版,95题增加了构建出所有树的要求,而不仅仅是计算树的数量。对于96题,我们可以轻易得出树数量的规律。即:
1.长度为3的树可由根和一个长度为2的子树构成(即以3为根或者以1为根) 或者 由根和两个长度为1的子树构成(
即以2为根)
2. 长度为4的子树可由根和一个长度为3的子树构成(即以4或者1为根) 或者 由根和一个长度为1的子树和一个长度为2的子树构成(即以2为根或者3为根)
3. 可以发现,长度为n的树的数量取决于它能由多少种不同形式的子树构成。所以以此类推,便可得到递推公式。
以count作为分割左右子树的位置,即可得到以下的递推代码。
for count in range(i - 1, mid - 1, -1): sum += nums[count] * nums[i - count - 1]
回到95题,既然题目要求我们生成N棵子树,那么我们首先想到的必是先拷贝N-1棵子树,以root为新树的根,用roott源树的根进行拷贝。
注意:新树的跟必须要使用&引用值,因为root根一开始还未赋值,是为定义的值,需要在递归的过程赋值。若此处不使用&引用,则源程序在调用buildTree时,会给参数root拷贝一份源程序中root的值,而此时的root是未定义的,所以程序会报错。
TreeNode* buildTree(TreeNode*&root, TreeNode* roott) { if (!roott) return NULL; root = new TreeNode(roott->val); root->left = buildTree(root->left, roott->left); root->right = buildTree(root->right, roott->right); return root; }
拷贝完N-1棵子树后,自然是将新的值value放入前面已经构建完成的树中。例如,前面已经构建出 [2,1,NULL] 和 [1,NULL,2] 两棵树,那么当n=3的时候,我们需要将3插入这两棵树中。考虑到,我们是根据n值从小到大进行构建的,所以n值只能插入原树的右子树中,或者以n为根,所以我们可以得到建树算法。
/** * 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*> generateTrees(int n) { if (n == 0) return lastLevel; if (n == 1) { TreeNode* newNode = new TreeNode(1); lastLevel.push_back(newNode); return lastLevel; } TreeNode* root = new TreeNode(1); lastLevel.push_back(root); for (int i = 2; i < n + 1; ++i) { vector<TreeNode*> newLevel; for (int root = 0; root < lastLevel.size(); ++root) { TreeNode* newNode = new TreeNode(i); newNode->left = lastLevel[root]; newLevel.push_back(newNode); //查找根节点右子树的高度 int height = 0; TreeNode* temp = lastLevel[root]; while (temp->right != NULL) { height++; temp = temp->right; } //建立height + 1个新树,因为一个节点有两个插入位置 for (int h = 0; h < height + 1; h++) { TreeNode* newTreeNode = buildTree(newTreeNode, lastLevel[root]); getChild(newTreeNode, i, h); newLevel.push_back(newTreeNode); } } lastLevel = newLevel; if (i == n) { return lastLevel; } } }private: vector<TreeNode*> lastLevel; vector<TreeNode*> newLevel; TreeNode* buildTree(TreeNode*&root, TreeNode* roott) { if (!roott) return NULL; root = new TreeNode(roott->val); root->left = buildTree(root->left, roott->left); root->right = buildTree(root->right, roott->right); return root; } void getChild(TreeNode* root, int value, int height) { TreeNode *temp = root, *father = root; while (height >= 0) { father = temp; temp = temp->right; height--; } TreeNode* newNode = new TreeNode(value); father->right = newNode; newNode->left = temp; } void inOrder(TreeNode* root) { if (root) { inOrder(root->left); cout << root->val << ' '; inOrder(root->right); } }};
0 0
- 算法分析与设计第五周习题:95. Unique Binary Search Trees II
- 【算法分析与设计】【第十周】96. Unique Binary Search Trees
- [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
- Android性能优化之渲染优化
- Android NDK(三):JNI 字符串
- LeetCode 27. Remove Element 题解 —— Java
- hdu 1026
- JAVA回顾
- 算法分析与设计第五周习题:95. Unique Binary Search Trees II
- python tile函数
- C语言实现TCP服务器---模型(二)
- hdu 2612 Find a way
- 最长上升子序列(LIS) -最长公共子序列(LCS)
- 《ACM程序设计》书中题目W
- 怎么在Linux下启动mysql
- L题
- win7 64+python2.7.12安装numpy+scipy+matplotlib+scikit-learn