[LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
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
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
96. Unique Binary Search Trees 的扩展,96题只要算出所有不同BST的个数,这道题让把那些二叉树都表示出来。
用递归来解,划分左右子树,递归构造。
1. 根节点可以任取min ~ max (例如min = 1, max = n),假如取定为i。
2. 则left subtree由min ~ i-1组成,假设可以有L种可能。right subtree由i+1 ~ max组成,假设有R种可能。生成所有可能的left/right subtree。
3 对于每个生成的left subtree/right subtree组合<T_left(p), T_right(q)>,p = 1...L,q = 1...R,添加上根节点i而组成一颗新树。
Python:
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None def __repr__(self):
if self:
serial = []
queue = [self] while queue:
cur = queue[0] if cur:
serial.append(cur.val)
queue.append(cur.left)
queue.append(cur.right)
else:
serial.append("#") queue = queue[1:] while serial[-1] == "#":
serial.pop() return repr(serial) else:
return None class Solution:
# @return a list of tree node
def generateTrees(self, n):
return self.generateTreesRecu(1, n) def generateTreesRecu(self, low, high):
result = []
if low > high:
result.append(None)
for i in xrange(low, high + 1):
left = self.generateTreesRecu(low, i - 1)
right = self.generateTreesRecu(i + 1, high)
for j in left:
for k in right:
cur = TreeNode(i)
cur.left = j
cur.right = k
result.append(cur)
return result
C++:
class Solution {
public:
vector<TreeNode *> generateTrees(int n) {
return genBST(1, n);
}
vector<TreeNode *> genBST(int min, int max) {
vector<TreeNode *> ret;
if(min>max) {
ret.push_back(NULL);
return ret;
}
for(int i=min; i<=max; i++) {
vector<TreeNode*> leftSubTree = genBST(min,i-1);
vector<TreeNode*> rightSubTree = genBST(i+1,max);
for(int j=0; j<leftSubTree.size(); j++) {
for(int k=0; k<rightSubTree.size(); k++) {
TreeNode *root = new TreeNode(i);
root->left = leftSubTree[j];
root->right = rightSubTree[k];
ret.push_back(root);
}
}
}
return ret;
}
};
类似题目:
[LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树
All LeetCode Questions List 题目汇总
[LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II的更多相关文章
- LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
[096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...
- Leetcode96.Unique Binary Search Trees不同的二叉搜索树
给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 假设n个节点存在二叉排序树的 ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- leetcode 95 Unique Binary Search Trees II ----- java
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- jquery 表单对象属性筛选选择器
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...
- 【Herding HDU - 4709 】【数学(利用叉乘计算三角形面积)】
题意:给出n个点的坐标,问取出其中任意点围成的区域的最小值! 很明显,找到一个合适的三角形即可. #include<iostream> #include<cstdio> #in ...
- ES6新特性之 let 、const
在 ES6之前,ES5中js只有全局作用域和函数作用域,作用域是一个独立的地盘,让变量不外泄出去,但是上例中的变量就外泄了出去,所以此时 JS 没有块级作用域的概念. 全局作用域就是最外层的作用域,如 ...
- 从输入URL到页面返回的过程详解
文章转自以为大神的博客;https://www.cnblogs.com/xianyulaodi/p/6547807.html#_labelTop 总结的很不错,看完收获颇多, 下面就是大神的文章,我只 ...
- pandas 筛选
t={ , , np.nan, , np.nan, ], "city": ["BeiJing", "ShangHai", "Gua ...
- Vue移动端项目如何使用手机预览调试
- Git学习笔记--配置(二)
由之前文章,总结得出Git的特点: 最优的存储能力: 非凡性能: 开源的: 管理成本低: 很容易做备份: 支持离线操作: 很容易定制工作流程: Git is a free and open sourc ...
- fitnesse wiki界面设置变量
有时候我们可能多组测试数据会到同一个值,这样我们就可以设置一个变量,修改时只需要修改一个地方即可,而不需要对每组测试数据的这列数据进行修改 如下图: (1)定义变量:!define A {10} , ...
- React-Redux常见API
React-Redux是在Redux的基础上,将其大量重复的代码进行了封装. 1. Provider 利用context对象给所有子组件提供store.不再需要在每个组件都引用store. impor ...
- Pivotal Greenplum 6.0 新特性介绍
Pivotal Greenplum 6.0 新特性介绍 在1月12日举办的Greenplum开源有道智数未来技术研讨会上,Pivotal中国研发中心Greenplum 产品经理李阳向大家介绍了Pi ...