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
递归:
/* 设count[1,n]表示1~n可以组成的BST的个数。 num[i] 表示以i为根的BST个数,有num[i] = count[1,i-1]*count[i+1,n]; count[1,n] = sum(num[i]); */ public class Solution { public int numTrees(int n) { // Start typing your Java solution below // DO NOT write main() function if(n<1) return 0; return count(1,n); } public int count(int start,int end){ if(start == end) return 1; int count = count(start+1,end); count = count+count(start,end-1); for(int i = start+1;i<=end-1;i++){ count = count+count(start,i-1)*count(i+1,end); } return count; } }
Unique Binary Search Trees II
Aug 27 '123664 / 10952
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
与上题相比,需要输出所有符合条件的树的具体形式:
/*集合的笛卡尔乘积*/ public class UniqueBinaryTree2 { public ArrayList<TreeNode> generateTrees(int n) { // Start typing your Java solution below // DO NOT write main() function return generateTrees(1,n); } public ArrayList<TreeNode> generateTrees(int start,int end){ ArrayList<TreeNode> result = new ArrayList<TreeNode>(); if(start>end) {result.add(null);return result;} for(int i = start;i<=end;i++){ ArrayList<TreeNode> lefts = generateTrees(start,i-1); ArrayList<TreeNode> rights = generateTrees(i+1,end); for (int j = 0; j < lefts.size(); ++j) { for (int k = 0; k < rights.size(); ++k) { TreeNode root = new TreeNode(i); root.lchild = lefts.get(j); root.rchild = rights.get(k); result.add(root); } } } return result; } }
相关推荐
python python_leetcode题解之096_Unique_Binary_Search_Trees
python python_leetcode题解之095_Unique_Binary_Search_Trees_II
c语言基础 c语言_leetcode题解之0096_unique_binary_search_trees.zip
javascript js_leetcode题解之96-unique-binary-search-trees.js
c语言基础 c语言_leetcode题解之0095_unique_binary_search_trees_ii.zip
javascript js_leetcode题解之95-unique-binary-search-trees-ii.js
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
Unique Binary Search Trees 本题参考了 里面的*How Many Binary Trees Are There?*章节。 The first few terms: C(0) = 1 C(1) = C(0)C(0) = 1 C(2) = C(0)C(1) + C(1)C(0) = 2 C(3) = C(0)C(2) + C(1)C(1)
leetcode python 001 Algorithm 用python和java解决了Leetcode上部分问题,另外还有对常规算法和机器学习算法的一些实现,例如用遗传算法解物流配送问题。...Search Trees Python 2017/11/28 Medium 09
【描述】中的"Unique Binary Search Trees"提到了一种特定的LeetCode问题,即"唯一的二叉搜索树"。这个问题要求计算给定整数n的所有不同结构的二叉搜索树的数量。二叉搜索树是一种特殊的二叉树,其中每个节点的左...
- **唯一二叉搜索树(Unique Binary Search Trees)**: 不同的二叉搜索树的数量。 #### 位操作技巧 - **更新位(Update Bits)**: 在一个整数中更新若干位。 - **快速幂(Fast Power)**: 快速计算一个数的幂。 #### ...
leetcode添加元素使和等于 Leetcode-tree 94题 Binary ...Search Trees 这道题我们使用了动态规划的方法。说到动态规划我们这里先总结一下动态规划类型的题该如何做。 动态规划题目特点: 计数型: -有
96.unique-binary-search-trees.cpp [TODO] 70. 爬楼梯.cpp [TODO] 746. min-cost-climbing-stairs.cpp 第 4 天: [待办事项] leetcode/1143。 最长公共子序列.cpp 第 5 天: [待办事项] leetcode/221。 最大平方....
- Unique Binary Search Trees:不同的二叉搜索树的个数。 - Happy Number:一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,也可能是无限...