本文为大便一箩筐的原创内容,转载请注明出处,谢谢: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
OJ's Binary Tree Serialization:

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
OJ's 二叉树的序列化:

二叉树的序列化遵循水平阶遍历,“#”表示没有节点存在。

这儿有一个栗子 ⊙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的更多相关文章

  1. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  2. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  3. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  4. LeetCode - Unique Binary Search Trees II

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  5. 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 ...

  6. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  7. 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 ...

  8. LeetCode——Unique Binary Search Trees II

    Question Given an integer n, generate all structurally unique BST's (binary search trees) that store ...

  9. [Leetcode] Unique binary search trees ii 唯一二叉搜索树

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  10. [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 ...

随机推荐

  1. PHP多进程并行执行php脚本

    <?php //fork.php $cmds = [ '/data/wwwroot/default/test1.php', '/data/wwwroot/default/test2.php', ...

  2. Python3 打开 https 链接,异常:“SSL: CERTIFICATE_VERIFY_FAILED”

    Python3 打开 https 链接,异常:“SSL: CERTIFICATE_VERIFY_FAILED” 一.问题 Python2.7.9 之后,当使用urllib.urlopen打开一个 ht ...

  3. Linux LNMP架构搭建

    一.搭建LNMP基本架构 1.L(http) N(nginx) M(mysql) P(php) 2.安装顺序 Mysql-->PHP-->Nginx 3.安装包 Discuz_3. htt ...

  4. 解决Vue循环中子组件不实时更新的问题

    问题描述 使用Element-UI中的table组件时会遇到一个常见的问题.当在el-table中调用子组件的时候会出现数据更新后,子组件没有重新渲染的问题. eg:资源列表中的健康度组件. 代码如下 ...

  5. 牛客网试卷: 京东2019校招笔试Java开发工程师笔试题(1-)

    1.在软件开发过程中,我们可以采用不同的过程模型,下列有关 增量模型描述正确的是() A 是一种线性开发模型,具有不可回溯性 B 把待开发的软件系统模块化,将每个模块作为一个增量组件,从而分批次地分析 ...

  6. C# string字节数组转换

    string转byte[]:byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转string:string ...

  7. Linux 安装 mysql 转 http://www.cnblogs.com/fnlingnzb-learner/p/5830622.html

    到mysql官网下载mysql编译好的二进制安装包,在下载页面Select Platform:选项选择linux-generic,然后把页面拉到底部,64位系统下载Linux - Generic (g ...

  8. 论文笔记——PRUNING FILTERS FOR EFFICIENT CONVNETS

    论文地址:https://arxiv.org/abs/1608.08710 主要思想 这篇文章主要讲了对filters的裁剪,裁剪方法是计算L1范数,然后裁剪掉较少的,多少取决于加速比. 实现效果 V ...

  9. Nlog、elasticsearch、Kibana以及logstash在项目中的应用(一)

    前言 最近在做文档管理中,需要记录每个管理员以及用户在使用过程中的所有操作记录,本来是通过EF直接将操作数据记录在数据库中,在查询的时候直接从数据库中读取,但是这样太蠢了,于是在网上找到了logsta ...

  10. Windows下搭建FTP服务器

    一.什么是ftp? FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”.用于Internet上的控制文件的双向传输.同时,它也是一个应用程序(A ...