LeetCode: Unique Binary Search Trees [095]
【题目】
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
【题意】
给定一个数字n, 问用1,2,3,4,5...n这n个值,能构造多少棵合法的二叉搜索树
【思路】
对于给定[1,n]区间,先确定全部可能的根。如果根为k, 则该二叉树左子树的取值区间为[1, k-1]和右子树的取值区间[k+1, n]。
而二叉搜索搜索树的随意一个节点的左右子树也是二叉搜索树,因此我们须要确定[1,k-1]和[k+1, n]上构造的二叉搜索树的数目, 比方分别为left[k], right[k]。
则以k的根的二叉搜索树的数目即为,left[k]*right[k]
本题用递归来解决。
【代码】
class Solution {
public:
int binaryTreeNums(int start, int end){
//[start, end]区间上构造二叉树的数目
if(start>=end)return 1; //start<end表示空子树, start==end表示叶子节点
int treeNums=0;
for(int root=start; root<=end; root++){
int leftCount = binaryTreeNums(start, root-1); //计算左子树的数目
int rightCount = binaryTreeNums(root+1, end); //计算右子树的数目
treeNums+= leftCount*rightCount;
}
return treeNums;
}
int numTrees(int n) {
if(n==0)return 0;
return binaryTreeNums(1, n);
}
};
LeetCode: Unique Binary Search Trees [095]的更多相关文章
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode - Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- 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 ...
- [leetcode]Unique Binary Search Trees @ Python
原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...
- LEETCODE —— Unique Binary Search Trees [动态规划]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Leetcode Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- poj 1845 POJ 1845 Sumdiv 数学模板
筛选法+求一个整数的分解+快速模幂运算+递归求计算1+p+p^2+````+p^nPOJ 1845 Sumdiv求A^B的所有约数之和%9901 */#include<stdio.h>#i ...
- 出现异常 child->m_pParent == 0
在cocos2d-x中,能够用CCNode类 自己new一个节点(或是用CCnode::node().create()),当将它作为其它若干item(如button项.sprite项.image项)的 ...
- Windbg抓取程序崩溃的dmp文件的方法
Windbg抓取程序崩溃的dmp文件的方法 一. 简介 windbg是在windows平台下,强大的用户态和内核态调试工具.相比较于Visual Studio,它是一个轻量级的调试工具, ...
- Practical Common Lisp
Practical Common Lisp Practical Common Lisp
- “HTTP 错误 401.1 - 未授权:登录失败” iis配置和权限问题
今天,将项目发布到IIS服务器上,出现此问题,本地IIS访问正常. 登录失败说明根本登录不了,谈何访问网页,所以要从两方面下手,一.账户:二.账户权限: 一.设置你网站属性的时候,目录安全性-匿名访问 ...
- ios block循环引用问题
ios开发中,开了ARC模式,系统自动管理内存,如果程序中用到了block就要注意循环引用带来的内存泄露问题了 这几天遇到一个问题,正常页面dismiss的时候是要调用dealloc方法的,但是我的程 ...
- [Android学习笔记]View的measure过程学习
View从创建到显示到屏幕需要经历几个过程: measure -> layout -> draw measure过程:计算view所占屏幕大小layout过程:设置view在屏幕的位置dr ...
- [Android学习]Activity之间传递对象和对象集合
开发过程中,Activity之间传递数据是必不可少的,android中使用Intent和Bundle作为数据载体,在Activity之间传递,对于基础数据类型,Bundle已经提供相关的put,get ...
- python大文件迭代器的流式读取,之前一直使用readlines()对于大文件可以迅速充满内存,之前用法太野蛮暴力,要使用xreadlines或是直接是f,
#!/usr/bin/env python #encoding=utf-8 import codecs count =0L #for line in file("./search_click ...
- Cocos2d-x-lua游戏两个场景互相切换MainScene01切换到MainScene02
/* 场景一lua代码 */ require "MainScene02" local dic_size = CCDirector:sharedDirector():getWinSi ...