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. Discuz常见小问题-如何修改板块和分区

    1 论坛-版块管理,然后添加一个版块名称(我把版块名称跟前面的主导航对应起来,比如都是论坛首页,在论坛首页下面放了三个版块,最新产品信息,最新培训信息,最新专题讨论) 2 点击编辑按钮 3 如果我设置 ...

  2. boost::tie()和boost::variant()解说

    #include<iostream> #include<boost/tuple/tuple.hpp> #include<boost/variant.hpp> #in ...

  3. Java连接Oracle数据库示例

    1.数据库复用模块 package cn.jzy.database; import java.sql.Connection; import java.sql.DriverManager; import ...

  4. iOS多线程与网络开发之多线程NSThread

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...

  5. Ubuntu 源码方式安装Subversion

    使用到的安装包: apr-1.5.1.tar.gz apr-util-1.5.3.tar.gz pcre-8.35.tar.gz httpd-2.4.9.tar.bz2 subversion-1.8. ...

  6. rpm常用命令及rpm参数介绍

    RPM是RedhatPackageManager的缩写,是由RedHat公司开发的软件包安装和管理程序,同Windows平台上的Uninstaller比较类似.使用RPM,用户可以自行安装和管理Lin ...

  7. MySQL 设置慢查询为200ms

    1:查看当前版本并设置long_query_time为0.2 mysql> select version(); +------------+ | version() | +----------- ...

  8. 【CMS】安装CMS

    Apache和MySQL准备好后开始安装CMS 在浏览器输入:http://localhost/install/index.php即可开始安装.

  9. vue 子组件引用

    使用 ref 为子组件指定一个引用 ID.例如: <div id="parent"> <user-profile ref="profile"& ...

  10. python之函数用法id(),了解即可

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法id(),了解即可 #http://www.cnblogs.com/hongfei/p ...