UVA 1264 - Binary Search Tree(BST+计数)

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;
}
时间: 2024-08-09 22:02:43

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

uva 1264 - Binary Search Tree(BST)

题目链接:uva 1264 - Binary Search Tree 题目大意:给定一个插入顺序,要求输出有多少种插入顺序,使得生成的BST一样. 解题思路:组合数学+BST的性质,起始左右两个子树的节点之间是没有影响的.所以逐层递推上去即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int max

UVA - 10304Optimal Binary Search Tree(递推)

题目:UVA - 10304Optimal Binary Search Tree(递推) 题目大意:给出一组数,e1 < e2 < ... < en,现在要求将这些数组成一棵二叉搜索树,并且使得sum (ei * cost(ei))最小.cost(ei)表示ei到到根节点之间有多少条边. 解题思路:首先二叉搜索树要满足左节点小于根节点,右节点大于根节点.因此对于e1 < e2 < ... < en这样一组数,我们只要枚举根节点的位置ek,将这个序列分成左右子树两部分(e

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* LCA(Node* root, Node* p, Node* q) { if (!root || !p || !q) return NULL; if (max(p->data, q->data) < root->data) return LCA(root->left, p,

【LeetCode】 Recover Binary Search Tree BST 中序遍历

题目:Recover Binary Search Tree <span style="font-size:18px;">/* * LeetCode: recover the binary search tree * 题目:二叉树中有两个节点被交换了位置,找出它们,并且将它们换回来,要求用o(n)的连续空间 * 知识点:1.BST树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

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 where elements are sorted in ascending order, convert it to a height balanced BST. Code: BinaryTree* sortedListToBST(ListNode *& list, int start, int

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 circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Code: v

Binary Search Tree BST Template

Use one queue + size variable 1 public class Solution { 2 public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) { 3 ArrayList result = new ArrayList(); 4 if (root == null) 5 return result; 6 Queue<TreeNode> queue = new LinkedLis

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 -->6 2,4-–>2 原文描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA

501. Find Mode in Binary Search Tree查找BST中的众数

[抄题]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to t