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. listView/GridView getChild获取不到的解决方法

    在onCreate或onResume中调用了getChildAt()方法,这时候adapter中的Item还没有放入到AdapterView中去.... 解决方法,当activity获得焦点事件的时候 ...

  2. 算法笔记_208:第六届蓝桥杯软件类决赛真题(Java语言A组)

    目录 1 胡同门牌号 2 四阶幻方 3 显示二叉树 4 穿越雷区 5 切开字符串 6 铺瓷砖   前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 胡同门牌号 标题:胡同门牌号 小明家住在一条胡同里. ...

  3. 转:Python 从FTP 下载数据的例子

    <hadoop权威指南>的天气数据可以在ftp://ftp3.ncdc.noaa.gov/pub/data/noaa下载,在网上看到这个数据好开心,打开ftp发现个问题,呀呀,这么多文件啊 ...

  4. ubuntu中设置xampp开机启动

    sudo ln -s /opt/lampp/lampp /etc/init.d/lampp sudo update-rc.d -f lampp defaults

  5. python简单实现随机验证码

    这是自己在学习pyton时写的一个随机验证码,有不对的地方欢迎指出 代码 import random def verification(): lis = '' for i in range(4): S ...

  6. 从 高斯 到 正态分布 到 Z分布 到 t分布

    正态分布是如何被高斯推导出来的, 我感觉高斯更像是猜出了正态分布. 详见这篇文章:<正态分布的前世今生> http://songshuhui.NET/archives/76501 说一说理 ...

  7. 用javascript写一个emoji表情插件

    概述 以我们写的这个emoji插件为例,网上已经有一些相关的插件了,但你总感觉有些部分的需求不能被满足(如:可以自行添加新的表情包而不用去改源代码等等) 详细 代码下载:http://www.demo ...

  8. nexus 介绍

    http://juvenshun.iteye.com/blog/349534

  9. iOS升级swift3 遇到Overriding non-open instance method outside of its defining module的解决方案

    最近将我之前的一个swift项目升级swift3,说多了都是泪... 其中,遇到这样一个错误: 这是用的三方:ENSwiftSideMenu时引出的 报了两个错: 1.Cannot inherit f ...

  10. Python websocket

    一.自己实现websocket 网上流传的都是Python2的websocket实现 # coding=utf8 # !/usr/bin/python import struct, socket im ...