本文为大便一箩筐的原创内容,转载请注明出处,谢谢: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. 开发代码code中变量替换

    除了automake/autoconfig 之外,还有其他的替换方式. 参看vdsm https://github.com/oVirt/vdsm/blob/master/Makefile.am htt ...

  2. go learning

    1. vim-go https://github.com/fatih/vim-go-tutorial curl -fLo ~/.vim/autoload/plug.vim --create-dirs ...

  3. 任务调度之Timer与TimerTask配合

    什么是任务调度? 在实际业务中,我们经常需要定时.定期.或者多次完成某些任务,对这些任务进行管理,就是任务调度.任务调度与多线程密切相关. 任务调度有多种方式 Timer与TimerTask配合 Ti ...

  4. C# 测算代码运行时间 Stopwatch

    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); stop ...

  5. 关于使用jquery的Ajax结合java的Servlet后台判定用户名是否存在

    关于把AJAX加入到注册登录demo中去 2018年3月10日 19:21:23 第一次来SUBWAY真切地打代码. 这次的西红柿汤还是挺好喝的. index.jsp: <%@ page con ...

  6. Cent OS 常用配置命令

    1.ifconfig   #查看网络接口状态 2.ifconfig –a  #查看主机所有接口的情况 3.ifconfig eth0 192.168.1.106 netmask 255.255.255 ...

  7. kaggle CTR预估

    参考涛哥之前做过的CTR预估project,学习下CTR预估的相关知识:http://blog.csdn.net/hero_fantao/article/category/6877765 目标:本周末 ...

  8. C#修饰符说明

    方法不加访问修饰符默认的是 private 类不加访问修饰答默认的是 internal //////////////////////////////////////////////////////// ...

  9. MongoDB(课时14 正则运算)

    3.2.4.9 正则运算 如果想实现模糊查询,必须使用正则表达式,而且正则表达式使用的语言是Perl兼容的正则表达式的形式. 要实现正则使用,则按照如下的定义格式: 基础语法:{key : 正则标记} ...

  10. JAVA反射会降低你的程序性能吗?

    原文出处 早两天写了<从把三千行代码重构成15行代码谈起>这篇文章,看到评论中有一些同学的回复还是在质疑反射的性能,好像程序用上了反射,就像开上了拖拉机似的.本来我觉得这个话题没有什么好讨 ...