随笔分类 - 树
摘要:Mobile Computing枚举二叉树然后计算其宽度即可,每次枚举两个节点构造一个父节点,计算宽度时需要注意的是每棵树的左节点的右边缘可能超过其右子树的左边缘,反之亦然。#includeusing namespace std;const int maxn = 12...
阅读全文
摘要:Spatial Structures#includeusing namespace std;const int maxn = 64 + 5;int n,len;int seq[maxn*maxn];char img[maxn][maxn];struct node{ ...
阅读全文
摘要:S-Trees#includeusing namespace std;const int maxn = 7;int n,m,t,cnt = 0;char s[2];int a[maxn];char b[maxn];char leaf[int(pow(2,maxn))]...
阅读全文
摘要:Tree Recovery#includeusing namespace std;const int maxn = 30;struct node{ char c; struct node* l = NULL; struct node* r = NUL...
阅读全文
摘要:The Falling Leaves这道题还是递归,仿照上道题的思路挺容易的,不过看了看书上的代码个人觉得我的代码更简洁,思路更清晰些哈哈。我的思路:开个负下标数组,记录每个位置的权重之和,用 p 记录当前节点位置,那么其左节点位置为 p - 1,右节点位置为 p +...
阅读全文
摘要:Not so Mobile 我的思路:可以将天平看做一棵二叉树,二叉树的每个节点要记录其父节点。然后其实就是一个建树的过程,遇到 0 节点就向下延伸,每当一个节点的左右子树确定(其重量也随之确定),就计算其是否平衡,然后一直向上追溯,直到该节点尚未平衡,继续建树,直至...
阅读全文
摘要:Tree首先根据中序遍历和后序遍历递归地构造二叉树(后续遍历的最后一个节点为二叉树的根节点),然后前序遍历。输入有点坑,你说你直接告诉多少个节点多好。。。我想的是先用fets读取整个串,然后用atoi转化为数字,本地运行没问题可是一提交就报错,暂时还没找到原因,大概是...
阅读全文
摘要:Trees on the level 哈哈 这次是一次AC,好开心啊version 1(指针链表版):#includeusing namespace std;const int maxn = 256 + 5;struct node{ int num = -1; ...
阅读全文
摘要:Dropping Balls直接模拟似乎很简单,但是会超时。。。每个小球都会落在根结点上,因此前两个小球必然是一个在左子树,一个在右子树。一般地,只需看小球编号的奇偶性,就能知道它是最终在哪棵子树中。对于那些落入根结点左子树的小球来说,只需知道该小球是第几个落在根的左...
阅读全文
摘要:05-树9 Huffman Codes(30 分) In 1953, David A. Huffman published his paper “A Method for the Construction of Minimum-Redundancy Code...
阅读全文
摘要:05-树8 File Transfer(25 分) We have a network of computers and a list of bi-directional connections. Each of these connections allo...
阅读全文
摘要:05-树7 堆中的路径(25 分) 将一系列给定数字插入一个初始为空的小顶堆H[]。随后对任意给定的下标i,打印从H[i]到根结点的路径。输入格式:每组测试第1行包含2个正整数N和M(≤1000),分别是插入元素的个数、以及需要打印的路径条数。下一行给出区间...
阅读全文
摘要:04-树6 Complete Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the followin...
阅读全文
摘要:04-树5 Root of AVL Tree(25 分)An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees ...
阅读全文
摘要:04-树4 是否同一棵二叉搜索树(25 分) 给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果。于是对于输...
阅读全文
摘要:03-树3 Tree Traversals Again(25 分)An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example,...
阅读全文
摘要:03-树2 List Leaves(25 分) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.Input S...
阅读全文
摘要:03-树1 树的同构(25 分)给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。图1图2现给定两...
阅读全文