【LeetCode】Unique Binary Search Trees II 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html
原题:
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
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}"
.
解释:
对给定的数字 n,生成所有储存了数字 1 至 n 的结构不同的 BST's (二叉查找树)。
举个栗子 ⊙o⊙,
假如给定的 n = 3,你的程序应该返回如下图所示的5个不同的二叉查找树。
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
二叉树的序列化遵循水平阶遍历,“#”表示没有节点存在。
这儿有一个栗子 ⊙o⊙ :
1
/ \
2 3
/
4
\
5
上图的二叉树序列化后的结果为“{1,2,3,#,#,4,#,#,5}”。
思路:
这道题是 Unique Binary Search Trees 的升级版,解决方法同样是动态规划。
在做 Unique Binary Search Trees 这道题时,我们用一个数组保存 1 至 n-1 对应的不同二叉树的个数 X1、X2、X3、... Xn-1,
则 n 对应的不同二叉树个数Xn = Xn-1 + X1*Xn-2 + X2*Xn-3 + X3*Xn-4 + ... + Xn-2*X1 + Xn-1
通过这个递推式,我们可以从 N = 1 开始递推,最后得到 N = n 时不同二叉查找树的个数。
与上述思路类似,我们可以通过深度优先搜索(递归)解决这道题。
因为二叉查找树满足父节点的值大于左子节点的值,小于右子节点的值,所以我们可以:
(1) 从 N=1 开始构建二叉查找树,则它的左子树节点数为 0,右子树节点数为 n-1;
(2) N=2 时,左子树节点数为 1,右子树节点数为 n-2;
……
(n) N=n 时,左子树节点数为 n-1,右子树节点数 0。
而在第(1)步中,右子树继续执行上述循环,子树的子树又执行这个循环,最终,我们可以将子树节点数减少到 1,而一个节点只有一种排列方式,所以此时可以毫不犹豫地将结果返回给上一级。然后包含有两个节点的二叉树排列方式又被返回给上一级。……
依此类推,我们最后可以得到所有不同结构的二叉查找树。
源码:
// Author DaBianYiLuoKuang.
// http://www.cnblogs.com/dbylk/ class Solution {
public:
vector<TreeNode *> generateTrees(int n) {
return GenerateSubTree(, n + );
} vector<TreeNode*> GenerateSubTree(int l, int r) {
vector<TreeNode *> subTree; if (l >= r) {
subTree.push_back(NULL);
return subTree;
} if (l == r - ) {
subTree.push_back(new TreeNode(l));
return subTree;
} for (int i = l; i < r; ++i) {
vector<TreeNode *> leftSubTree = GenerateSubTree(l, i);
vector<TreeNode *> rightSubTree = GenerateSubTree(i + , r); for (int m = ; m < leftSubTree.size(); ++m) {
for (int n = ; n < rightSubTree.size(); ++n) {
TreeNode *root = new TreeNode(i);
root->left = leftSubTree[m];
root->right = rightSubTree[n];
subTree.push_back(root);
}
}
} return subTree;
}
};
【LeetCode】Unique Binary Search Trees II 异构二叉查找树II的更多相关文章
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode - Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 96. Unique Binary Search Trees(I 和 II)
Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example ...
- LeetCode——Unique Binary Search Trees II
Question Given an integer n, generate all structurally unique BST's (binary search trees) that store ...
- [Leetcode] Unique binary search trees ii 唯一二叉搜索树
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] Unique Binary Search Trees II dfs 深度搜索
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
随机推荐
- NFS Iptables放行服务端口
启动NFS会开启如下端口:1)portmapper 端口:111 udp/tcp:2)nfs/nfs_acl 端口:2049 udp/tcp:3)mountd 端口:"32768--6553 ...
- CAN/J1850/
(1)CAN:(差分信号)有信号CANH=3.5V,CANL=1.5V, 没有信号CANH=2.5V,CANL=2.5V 速率:CAN系统又分为高速和低速,高速CAN系统采用硬线是动力型,速度:500 ...
- 维特比算法Python实现
前言 维特比算法是隐马尔科夫问题的一个基本问题算法.维特比算法解决的问题是已知观察序列,求最可能的标注序列. 什么是维特比算法? 维特比算法尽管是基于严格的数学模型的算法,但是维特比算法毕竟是算法,因 ...
- java.lang.NoClassDefFoundError错误
根据前文,很明显NoClassDefFoundError的错误是因为在运行时类加载器在classpath下找不到需要加载的类,所以我们需要把对应的类加载到classpath中,或者检查为什么类在cla ...
- Python3基础 str *运算 重复拼接字符串
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Excel中Application和ApplicationClass的区别
Application和ApplicationClass的联系和区别Application和ApplicationClass都继承自接口_Application.Application为接口.Appl ...
- 《EMCAScript6入门》读书笔记——22.Module的语法
- Mybatis配置映射文件中parameterType的用法小结
原创: 在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType为输入参数,在配置的时候,配 ...
- Nginx 随笔
使用包管理工具安装nginx Linux(基于deb) sudo apt-get install nginx Linux(基于rpm) sudo yum install nginx FreeBSD s ...
- Python简单做二维统计图
先上一张效果图: 以上图是一段时间内黄金价格的波动图. 代码如下: import datetime as DT from matplotlib import pyplot as plt from ma ...