Unique Binary Search Trees:求生成二叉排序树的个数。

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

For example,
Given n = 3, there are a total of 5 unique BST's.

   1         3     3      2      1\       /     /      / \      \3     2     1      1   3      2/     /       \                 \2     1         2                 3

算法分析:类似上阶梯,简单的动态规划问题。当根节点为i时,比i小的节点有i-1个,比i大的节点有n-i个,所以,i为根节点能够生成二叉排序树的个数是

nums[n] += nums[i-1]*nums[n-i],i从1到n。

public class UniqueBinarySearchTrees
{public int numTrees(int n){if(n <= 0){return 0;}int[] res = new int[n+1];res[0] = 1;res[1] = 1;for(int i = 2; i <= n; i ++){for(int j = 1; j <= i; j ++)//j为根节点{res[i] += res[j-1]*res[i-j];}}return res[n];}
}

Unique Binary Search Trees2:求生成二叉排序树的根节点的集合

Given an integer 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

算法分析:这个不是求个数,而是求生成树根节点。使用递归。

public class UniqueBinarySearchTreesII
{public List<TreeNode> generateTrees(int n) {if(n <= 0){return new ArrayList<TreeNode>();}return helper(1, n);}public List<TreeNode> helper(int m, int n){List<TreeNode> res = new ArrayList<>();if(m > n){res.add(null);return res;}for(int i = m; i <= n; i ++){//i为根节点List<TreeNode> ls = helper(m, i-1);//i节点的左子树List<TreeNode> rs = helper(i+1, n);//i节点的右子树for(TreeNode l : ls){for(TreeNode r : rs){TreeNode curr = new TreeNode(i);curr.left = l;curr.right = r;res.add(curr);}}}return res;}
}

转载于:https://www.cnblogs.com/masterlibin/p/5827259.html

Unique Binary Search Trees,Unique Binary Search Trees2 生成二叉排序树相关推荐

  1. [LeetCode]: 96: Unique Binary Search Trees

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

  2. 【Unique Binary Search Trees II】cpp

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

  3. LeetCode: Unique Binary Search Trees [095]

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

  4. 【LeetCode从零单排】No96 Unique Binary Search Trees

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

  5. LeetCode:Unique Binary Search Trees

    问题描写叙述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? F ...

  6. leetcode 95. Unique Binary Search Trees II | 96. Unique Binary Search Trees

    95. Unique Binary Search Trees II https://leetcode.com/problems/unique-binary-search-trees-ii/ 题解 题是 ...

  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. [swift] LeetCode 96. Unique Binary Search Trees

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

  9. LeetCode 96. Unique Binary Search Trees

    96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...

  10. Unique Binary Search Trees ll -深度优先遍历DFS

    题目:Unique Binary Search Trees ll English: Given an integer n, generate all structurally unique BST's ...

最新文章

  1. 学会理解并编辑/etc/fstab
  2. Android进程间通信(IPC)机制Binder简要介绍和学习计划
  3. CG-CTF-Web-bypass again
  4. Cognos报表展示图片小技巧
  5. [异常解决] ubuntu上安采用sudo启动的firefox,ibus输入法失效问题解决
  6. 协议转换器的特点有哪些?
  7. 外媒称苹果挖走特斯拉高管 可能将重启电动汽车开发
  8. 订阅发布系统得解耦与冗余
  9. 在线民宿满意度测评项目[开源]
  10. windows下 MongoDB 导入json文件
  11. Web---HTTP请求、重定向、转发和数据压缩
  12. 830计算机软件综合大纲,2019年中国地质大学830计算机软件综合考试大纲.pdf
  13. 转载《2020年校招面经心得》
  14. Android开发 环境搭建(ubuntu)
  15. Oracle语句函数
  16. android平台多路摄像头实现方式的简析
  17. 微型计算机原理考试试卷,微机原理试题集试题库(带答案解析)
  18. 华为k662c的虚拟服务器,华为k662c光猫怎么样? 华为K662c拆机技巧
  19. 带2nd的计算机软件,2nd在计算器中是什么意思
  20. fest556_FEST JavaFX编译器Maven插件

热门文章

  1. 个人的一些MySql管理经验
  2. iOS NSURLSession VS NSURLConnection
  3. Unity3D场景漫游以及碰撞防止反弹
  4. jQuery具体实例介绍什么时候用ajax,ajax应该在什么地方使用
  5. everything is tools!
  6. Oracle 权限管理
  7. XP下如何引导Vista
  8. Action 和Func
  9. idea中通过搜索添加maven依赖包
  10. leftjoin多了性能下降_SBR胶乳改性剂用量对乳化沥青及微表处性能影响