本文为大便一箩筐的原创内容,转载请注明出处,谢谢: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. JavaScript 获取和修改 内联样式

    JavaScript 获取和修改 内联样式 版权声明:未经授权,严禁转载分享! 元素的样式 HTML 元素的 style 属性返回一个 CSSStyleDeclaration 类型的对象. Style ...

  2. mac下的一些操作

    mac 下修改Hosts文件 : http://www.cnblogs.com/zhangqs008/p/3773623.html mac下装Tomcat服务器: 在苹果系统安装Tomcat:首先下载 ...

  3. warning C4018: “<”: 有符号/无符号不匹配

    原因: 将两个不同的类型进行了比较,如: int a:unsigned short b: if(a>b)... 解决:改为同一种类型

  4. 如何高效判断java数组是否包含某个值

    在java中,我们如何判断一个未排序数组中是否包含一个特定的值?这在java中是一个频繁非常实用的操作.那么什么样的方法才是最高效的方式?当然 ,这个问题在Stack Overflow也是得票率非常高 ...

  5. cogs 1962. [HAOI2015]树上染色

    ★★☆   输入文件:haoi2015_t1.in   输出文件:haoi2015_t1.out   简单对比 时间限制:1 s   内存限制:256 MB [题目描述] 有一棵点数为N的树,树边有边 ...

  6. U盘+GRUB2引导PE或linux镜像

    利用U盘制作启动盘,引导WinPE.Linux安装的文章多如牛毛,与他们相比本文的特点有: 1.用且仅用grbu2做引导.不使用grub4dos,不是使用msdos mbr转到活动分区,到ntldr, ...

  7. chromedriver下载安装

    博主开发平台是win10,Python版本是3.6.最近需要用到chromedriver+selenium,下载好selenium后,pip install chromedriver,直接安装到pyt ...

  8. Java中一种无意识的递归

    来自: Java编程思想P287 public class Main { /** * @param args */ @Override public String toString() { retur ...

  9. ubuntu14.04(server amd64)免密码sudo

    vi /etc/sudoers.d/nopasswd4sudo 加入以下内容 用户名 ALL=(ALL) NOPASSWD : ALL

  10. windows服务部署

    1.新建windows服务项目 2.编辑业务代码 我这里只写2句记录文本的测试代码 using System; using System.IO; using System.ServiceProcess ...