POJ 1577 Falling Leaves 二叉搜索树】的更多相关文章

HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tr…
http://poj.org/problem?id=1577 [题意] 有一颗二叉搜索树,每次操作都把二叉搜索树的叶子从左到右揪掉(露出来的父节点就变成了新的叶子结点) 先给出了揪掉的叶子序列(多个字符串) 给出这课二叉搜索树先序遍历的结果 [思路] 最后揪掉的肯定是根,最后揪掉的是最先插入的,倒着建立二叉搜索树就可以. [AC] #include<iostream> #include<cstdio> #include<cstring> #include<stri…
思路:当时学长讲了之后,似乎有点思路----------就是倒着建一个  二叉搜索树 代码1:超时 详见超时原因 #include<iostream> #include<cstring> #include<stdio.h> using namespace std; char c[100][100]; struct node { char c; node *lchild; node *rchild; }; void f(node *p)//前序遍历输出,用递归 { pri…
题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D), 可以知道在根结点左边的为左子树结点(ABC),右边为右子树结点(EFG):可以求出左子树与右子树结点个数:已知道左右子树 结点个数(分别为3个),根据先序遍历(如DBACEGF)可知其左子树的先序遍历(BAC)与右子树的先序遍历(EGF):左右子树 的先序遍历与中序遍历可知,递归构造树再后序遍…
题意:给出一棵字母二叉树删除叶子节点的序列,按删除的顺序排列.让你输出该棵二叉树额前序遍历的序列.思路:先把一棵树的所有删除的叶子节点序列存储下来,然后从最后一行字符串开始建树即可,最后遍历输出.    这里为方便起见,将子母转化成整数值存储. #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> /* AC 题意:给出一棵字母二叉树删除叶子节点的序列,…
题意:给出一些字符串,从上到下的建树,输出其前序遍历 像前面那一题一样,先建树,然后再递归前序遍历 不过想像上一题那样用数组建树,建树和上题一样的办法,可是应该怎么输出前序遍历呢= = 还是看的题解= = #include<iostream> #include<cstdio> #include<cstring> #include <cmath> #include<algorithm> using namespace std; typedef lo…
思路:除以2^k,找到商为奇数的位置,k为层数,有2^(k+1)-1个节点 这里直接用位运算,x & -x 就求出 2^k 了. #include<iostream> using namespace std; long lowbit(long x) { return x & -x; } int main() { long n,x; cin>>n; while(n--) { cin>>x; cout<<x-lowbit(x)+1<<…
题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的... //#include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <iostream> using namespace std; struct BST { char name…
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现.由于篇幅有限,此处仅作一般介绍(如果想要完全了解二叉树以及其衍生出的各种算法,恐怕要写8~10篇). 1)二叉树(Binary Tree) 顾名思义,就是一个节点分出两个节点,称其为左右子节点:每个子节点又可以分出两个子节点,这样递归分叉,其形状很像一颗倒着的树.二叉树限制了每个节点最多有两个子节…
03-树1. 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 Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) wh…