UVA 1264 - Binary Search Tree

题目链接

题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列)

思路:先建树,然后dfs一遍,对于一个子树而言,仅仅要保证左边和右边顺序对就能够了,所以种数为C(左右结点总数,左结点),然后依据乘法原理乘上左右子树的情况就可以

代码:

#include <cstdio>
#include <cstring> typedef long long ll; const int MAXNODE = 1111111; const int N = 21;
const int MOD = 9999991; int C[N][N]; struct BST {
struct Node {
int l, r, val, lsz, rsz;
Node() {l = 0, r = 0, val = -1; lsz = 0; rsz = 0;}
} node[MAXNODE]; int sz; void init() {
node[1] = Node();
sz = 2;
} void insert(int x, int v) {
if (node[x].val == -1) {
node[x].val = v;
return;
}
if (v < node[x].val) {
if (!node[x].l) {
node[sz] = Node();
node[x].l = sz++;
}
insert(node[x].l, v);
node[x].lsz++;
}
else {
if (!node[x].r) {
node[sz] = Node();
node[x].r = sz++;
}
insert(node[x].r, v);
node[x].rsz++;
}
} int dfs(int x) {
if (x == 0) return 1;
return (ll)dfs(node[x].l) * dfs(node[x].r) % MOD * C[node[x].lsz + node[x].rsz][node[x].lsz] % MOD;
} void solve() {
init();
int n, num;
scanf("%d", &n);
while (n--) {
scanf("%d", &num);
insert(1, num);
}
printf("%d\n", dfs(1));
} } gao; int t; void getC() {
for (int i = 0; i < N; i++) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++)
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
} int main() {
getC();
scanf("%d", &t);
while (t--) {
gao.solve();
}
return 0;
}

UVA 1264 - Binary Search Tree(BST+计数)的更多相关文章

  1. Lowest Common Ancestor of a Binary Search Tree (BST)

    Given a binary search tree(BST), find the lowest common ancestor of two given nodes in the BST. Node ...

  2. PAT 1099 Build A Binary Search Tree[BST性质]

    1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  3. Convert Sorted List to Balanced Binary Search Tree (BST)

    (http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list ...

  4. Convert Sorted Array to Balanced Binary Search Tree (BST)

    (http://leetcode.com/2010/11/convert-sorted-array-into-balanced.html) Given an array where elements ...

  5. Convert Binary Search Tree (BST) to Sorted Doubly-Linked List

    (http://leetcode.com/2010/11/convert-binary-search-tree-bst-to.html) Convert a BST to a sorted circu ...

  6. Binary Search Tree BST Template

    Use one queue + size variable public class Solution { public ArrayList<ArrayList<Integer>&g ...

  7. 【题解】【BST】【Leetcode】Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  8. 72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)

    题目描述: 一个二叉搜索树,给定两个节点a,b,求最小的公共祖先 _______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5 例如: 2,8 - ...

  9. (BST 递归) leetcode98. Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. VS2015不能修改安装路径问题

    Python作为一门强大.开源的脚本语言也被ArcGIS所使用,但其开发编程环境(IDE)实现是太不方便了,VS2015终于集成了python开发,所以,也想对python多作一些了解和使用. 但今天 ...

  2. ArcGIS安装问题集锦

    1. 软件安装 软件下载.安装问题自行解决,否则就不要使用. 2. 常见问题 2.1 许可管理器版本不正确 2013年3月19日 问题一:ArcGIS10安装后,更改许可管理器时,通常,在ArcGIS ...

  3. 网上下载的 chm 文件打开后右侧内容显示空白

    有时候在网上下载的chm文件打不开,或者打开后右侧内容显示空白,可尝试以下方法解决. 1.当你第一次打开文件时,会弹出如下警告窗口,点击打开: 打开后发现不管你怎么点,右边始终是空白的,有时候也会提示 ...

  4. POSIX 线程 – pthread_sigmask

    http://www.cnblogs.com/qq78292959/archive/2012/04/05/2432985.html 概念 按照 POSIX, 异步 (外部) 信号发送到整个进程. 所有 ...

  5. Jmeter相关

    关于Jmeter,这里有一篇文章可以看看:http://www.cnblogs.com/TankXiao/p/4045439.html 给有需要的同学.

  6. linux 目录结构(转)

    原文:http://www.centoscn.com/CentOS/2014/1222/4347.html linux 目录结构 /: 根目录,一般根目录下只存放目录,不要存放文件,/etc./bin ...

  7. javaweb笔记分享

    Lesson 1 一.eclipse工具的使用 1. java代码的位置 1) 选择工作空间 workspace  选择一个文件夹存放程序(代码) 不要用中文和空格 2) 新建一个java 工程(Pr ...

  8. Eclipse 如何导入web项目

      Eclipse 如何导入web项目 CreateTime--2018年3月8日09:07:16 Author:Marydon 方法一:推荐使用 1.将web项目手动拷贝到Eclipse的工作空间下 ...

  9. nginx反向代理proxy_set_header自定义header头无效

    公司使用nginx作为负载均衡,有时候需要自定义header头发送给后端的真实服务器. 想过去应该是非常的简单的事情. 例子如下: 设置代理服务器ip头   1 proxy_set_header X- ...

  10. cxf之org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cxf' available

    原因是.... 把cxf的配置文件spring-cxf-rest.xml配置结束后,没有import到spring.xml中...所以才加载不到bean.... 另附:异常org.springfram ...