题意:给你n和h,问有多少棵n个节点高度为h的二叉搜索树(标号为1-n,只有一个节点的树高为0),答案对10^9+7取模。
思路:设dp[ n ][ h ]为 n 个节点高度不超过 h 的二叉搜索树的个数。那么dpn,h=∑i=0n-1dpi,h?1?dpn?i-1,h?1
即选定一个点,枚举左子树的个数问为 i ,剩余右子树的个数即为n - 1 - i 。详见代码:
...
分类:
其他好文 时间:
2014-12-06 11:27:59
阅读次数:
267
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
深搜+递归
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* Tree...
分类:
其他好文 时间:
2014-12-05 17:34:59
阅读次数:
169
问题描述:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
基本思想:
二分法构建二差排序树。
代码:
TreeNode *subsortedArrayToBST(vector & num,int begin, int end) /...
分类:
其他好文 时间:
2014-12-04 23:18:01
阅读次数:
189
题意:
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....
分类:
其他好文 时间:
2014-12-04 21:38:52
阅读次数:
244
二叉排序树的查找算法
假定二叉排序树的根节点指针为root,给定的关键字值为K,则查找算法可描述为:
置初值:p = root ;
如果 key = p -> data ,则查找成功,算法结束;
否则,如果key data ,而且 p 的左子树非空,则将 p 的左子树根送 p ,转步骤 2 ;否则,查找失败,算法结束;
否则,如果 key > p->data ,而且...
分类:
编程语言 时间:
2014-12-04 12:16:31
阅读次数:
107
binary search tree,中文翻译为二叉搜索树、二叉查找树或者二叉排序树。简称为BST。
本文集齐了二叉树的五大遍历算法:先序遍历、中序遍历、后序遍历、深度优先遍历和广度优先遍历(同层遍历也就是深度优先遍历)。
// BSTree.h
#include
#include
#include
#include
using namespace std;
// bin...
分类:
编程语言 时间:
2014-12-04 01:02:39
阅读次数:
303
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is ...
分类:
其他好文 时间:
2014-12-03 21:14:01
阅读次数:
198
今天CSDN博客发生异常,折腾了大半天终于发出了这篇博文。
【题目】
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only no...
分类:
其他好文 时间:
2014-12-03 17:14:52
阅读次数:
219
问题描述:
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...
分类:
其他好文 时间:
2014-12-03 00:29:34
阅读次数:
136
基本概念
二叉查找树(Binary Search Tree),又称二叉排序树(Binary Sort Tree),亦称二查搜索书。
它或者是一棵空树;或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值; (2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值; (3)左、右子树也分别为二叉排序树;
简单的说就是:左孩子
因此...
分类:
其他好文 时间:
2014-12-02 20:53:19
阅读次数:
222